{"version":3,"file":"html.cjs","names":[],"sources":["../src/html/calc-scrollbar-width.ts","../src/html/check-element-has-parent.ts","../src/html/collect-offset-top.ts","../src/html/download-using-anchor.ts","../src/html/get-computed-color.ts","../src/html/get-element-inner-height.ts","../src/html/get-element-inner-width.ts","../src/html/global-scroll-into-view-for-y.ts","../src/html/is-prefers-dark-theme.ts","../src/html/is-prefers-light-theme.ts","../src/html/skip-event.ts","../src/html/start-view-transition-safety.ts","../src/html/wrap-text-to-tag-link.ts"],"sourcesContent":["/**\n * Calculates the scrollbar width.\n */\nexport const calcScrollbarWidth = (elementToAppend = document.body) => {\n  const outer = document.createElement('div');\n\n  outer.style.visibility = 'hidden';\n  outer.style.width = '100px';\n  outer.style.overflow = 'scroll';\n\n  elementToAppend.append(outer);\n\n  const inner = document.createElement('div');\n  inner.style.width = '100%';\n\n  outer.append(inner);\n\n  const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;\n\n  outer.parentNode?.removeChild(outer);\n\n  return scrollbarWidth;\n};\n","import type { Maybe } from 'yummies/types';\n\n/**\n * Checks whether the element is nested inside the provided parent element.\n *\n * @example\n * ```ts\n * checkElementHasParent(childElement, modalElement);\n * ```\n */\nexport const checkElementHasParent = (\n  element: HTMLElement | null,\n  parent: Maybe<HTMLElement>,\n) => {\n  let node = element;\n\n  if (!parent) return false;\n\n  while (node != null) {\n    if (node === parent) {\n      return true;\n    } else {\n      node = node.parentElement;\n    }\n  }\n\n  return false;\n};\n","/**\n * Collects the cumulative `offsetTop` value through the element parent chain.\n *\n * @example\n * ```ts\n * const offsetTop = collectOffsetTop(document.getElementById('section'));\n * ```\n */\nexport const collectOffsetTop = (element: HTMLElement | null) => {\n  let offsetTop = 0;\n  let node = element;\n\n  while (node != null) {\n    offsetTop += node.offsetTop;\n    node = node.parentElement;\n  }\n\n  return offsetTop;\n};\n","import { blobToUrl } from 'yummies/media';\n\n/**\n * Triggers a file download by creating and clicking a temporary anchor element.\n *\n * @example\n * ```ts\n * downloadUsingAnchor('/report.pdf', 'report.pdf');\n * ```\n */\nexport const downloadUsingAnchor = (\n  urlOrBlob: string | Blob,\n  fileName?: string,\n) => {\n  const url = blobToUrl(urlOrBlob);\n\n  const a = document.createElement('a');\n  a.href = url;\n\n  a.download = fileName ?? 'file';\n\n  a.target = '_blank';\n\n  document.body.append(a);\n\n  a.click();\n\n  a.remove();\n};\n","/**\n * Extracts an RGB value from any valid CSS color.\n *\n * Not recommended for frequent use because it triggers a reflow.\n */\nexport const getComputedColor = (color?: string): string | null => {\n  if (!color) return null;\n\n  const d = document.createElement('div');\n  d.style.color = color;\n  document.body.append(d);\n  const rgbcolor = globalThis.getComputedStyle(d).color;\n  const match =\n    /rgba?\\((\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*(,\\s*\\d+[.d+]*)*\\)/g.exec(rgbcolor);\n\n  d.remove();\n\n  if (!match) return null;\n\n  return `${match[1]}, ${match[2]}, ${match[3]}`;\n};\n","/**\n * Calculates the inner height of an HTML element, accounting for padding.\n */\nexport function getElementInnerHeight(element: HTMLElement) {\n  const { clientHeight } = element;\n  const { paddingTop, paddingBottom } = getComputedStyle(element);\n  return (\n    clientHeight -\n    Number.parseFloat(paddingTop) -\n    Number.parseFloat(paddingBottom)\n  );\n}\n","/**\n * Calculates the inner width of an HTML element, accounting for padding.\n */\nexport function getElementInnerWidth(el: HTMLElement) {\n  const { clientWidth } = el;\n  const { paddingLeft, paddingRight } = getComputedStyle(el);\n  return (\n    clientWidth -\n    Number.parseFloat(paddingLeft) -\n    Number.parseFloat(paddingRight)\n  );\n}\n","/**\n * Scrolls the page vertically to the viewport section containing the target element.\n *\n * @example\n * ```ts\n * globalScrollIntoViewForY(document.getElementById('footer')!);\n * ```\n */\nexport const globalScrollIntoViewForY = (node: HTMLElement) => {\n  const scrollContainer = document.body;\n  const pageHeight = window.innerHeight;\n  const nodeBounding = node.getBoundingClientRect();\n  const scrollPagesCount = scrollContainer.scrollHeight / pageHeight;\n\n  const scrollPageNumber = Math.min(\n    Math.max(nodeBounding.top / pageHeight, 1),\n    scrollPagesCount,\n  );\n\n  window.scroll({\n    top: scrollPageNumber * pageHeight,\n    behavior: 'smooth',\n  });\n};\n","/**\n * Checks whether the user prefers a dark color scheme.\n *\n * @example\n * ```ts\n * const prefersDark = isPrefersDarkTheme();\n * ```\n */\nexport const isPrefersDarkTheme = () =>\n  !!globalThis.matchMedia?.('(prefers-color-scheme: dark)')?.matches;\n","/**\n * Checks whether the user prefers a light color scheme.\n *\n * @example\n * ```ts\n * const prefersLight = isPrefersLightTheme();\n * ```\n */\nexport const isPrefersLightTheme = () =>\n  !!globalThis.matchMedia?.('(prefers-color-scheme: light)')?.matches;\n","/**\n * Prevents the default browser action and stops event propagation.\n *\n * @example\n * ```ts\n * button.addEventListener('click', (event) => skipEvent(event));\n * ```\n */\nexport const skipEvent = (e: Event) => {\n  e.preventDefault();\n  e.stopPropagation();\n\n  return false;\n};\n","/**\n * Executes a function within a view transition if supported by the browser.\n *\n * @param {VoidFunction} fn - The function to be executed.\n * @returns {ViewTransition} - The result of the executed function.\n *\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/startViewTransition | MDN: Document.startViewTransition}\n */\nexport const startViewTransitionSafety = (\n  fn: VoidFunction,\n  params?: { disabled?: boolean },\n) => {\n  if (\n    typeof document !== 'undefined' &&\n    document.startViewTransition &&\n    !params?.disabled\n  ) {\n    return document.startViewTransition(fn);\n  }\n  fn();\n};\n","/**\n * Surrounds string in an anchor tag\n */\nexport function wrapTextToTagLink(link: string) {\n  const descr = String(link).replace(/^(https?:\\/{0,2})?(w{3}\\.)?/, 'www.');\n  if (!/^https?:\\/{2}/.test(link)) link = `http://${link}`;\n  return `<a href=${link} target=\"_blank\">${descr}</a>`;\n}\n"],"mappings":";;;;;;;AAGA,IAAa,sBAAsB,kBAAkB,SAAS,SAAS;CACrE,MAAM,QAAQ,SAAS,cAAc,KAAK;CAE1C,MAAM,MAAM,aAAa;CACzB,MAAM,MAAM,QA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