@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
1 lines • 3.29 kB
Source Map (JSON)
{"version":3,"file":"find-element-in-shadow-dom.cjs","names":[],"sources":["../../../../src/core/utils/find-element-in-shadow-dom/find-element-in-shadow-dom.ts"],"sourcesContent":["export function findElementBySelector<T extends HTMLElement>(\n selector: string,\n root: Document | Element | ShadowRoot = document\n): T | null {\n // Directly try to find the element in the current root\n // querySelector searches all descendants in this root, but cannot cross shadow boundaries\n const element = root.querySelector<T>(selector);\n if (element) {\n return element;\n }\n\n // Find all elements in the current root to check for shadow roots\n // We need to search all descendants, not just direct children\n const allElements = root.querySelectorAll('*');\n for (let i = 0; i < allElements.length; i += 1) {\n const el = allElements[i];\n\n // Check if this element has a shadow root\n if ((el as Element).shadowRoot) {\n const shadowElement = findElementBySelector<T>(selector, (el as Element).shadowRoot!);\n if (shadowElement) {\n return shadowElement;\n }\n }\n }\n\n return null;\n}\n\nexport function findElementsBySelector<T extends HTMLElement>(\n selector: string,\n root: Document | Element | ShadowRoot = document\n): T[] {\n const results: T[] = [];\n\n // Collect all matching elements in the current root\n // querySelectorAll gets all descendants in this root, but cannot cross shadow boundaries\n const elements = root.querySelectorAll<T>(selector);\n results.push(...Array.from(elements));\n\n // Find all elements in the current root to check for shadow roots\n // We need to search all descendants, not just direct children\n const allElements = root.querySelectorAll('*');\n for (let i = 0; i < allElements.length; i += 1) {\n const el = allElements[i];\n\n // Check if this element has a shadow root\n if ((el as Element).shadowRoot) {\n const shadowElements = findElementsBySelector<T>(selector, (el as Element).shadowRoot!);\n results.push(...shadowElements);\n }\n }\n\n return results;\n}\n\n/**\n * Gets the appropriate root element (Document or ShadowRoot) for DOM queries\n * based on the provided target element reference.\n */\nexport function getRootElement(\n targetElement: HTMLElement | null | undefined\n): Document | ShadowRoot {\n if (!targetElement) {\n return document;\n }\n\n const root = targetElement.getRootNode();\n return root instanceof ShadowRoot || root instanceof Document ? root : document;\n}\n"],"mappings":";;AAAA,SAAgB,sBACd,UACA,OAAwC,UAC9B;CAGV,MAAM,UAAU,KAAK,cAAiB,SAAS;AAC/C,KAAI,QACF,QAAO;CAKT,MAAM,cAAc,KAAK,iBAAiB,IAAI;AAC9C,MAAK,IAAI,IAAI,GAAG,IAAI,YAAY,QAAQ,KAAK,GAAG;EAC9C,MAAM,KAAK,YAAY;AAGvB,MAAK,GAAe,YAAY;GAC9B,MAAM,gBAAgB,sBAAyB,UAAW,GAAe,WAAY;AACrF,OAAI,cACF,QAAO;;;AAKb,QAAO;;AAGT,SAAgB,uBACd,UACA,OAAwC,UACnC;CACL,MAAM,UAAe,EAAE;CAIvB,MAAM,WAAW,KAAK,iBAAoB,SAAS;AACnD,SAAQ,KAAK,GAAG,MAAM,KAAK,SAAS,CAAC;CAIrC,MAAM,cAAc,KAAK,iBAAiB,IAAI;AAC9C,MAAK,IAAI,IAAI,GAAG,IAAI,YAAY,QAAQ,KAAK,GAAG;EAC9C,MAAM,KAAK,YAAY;AAGvB,MAAK,GAAe,YAAY;GAC9B,MAAM,iBAAiB,uBAA0B,UAAW,GAAe,WAAY;AACvF,WAAQ,KAAK,GAAG,eAAe;;;AAInC,QAAO;;;;;;AAOT,SAAgB,eACd,eACuB;AACvB,KAAI,CAAC,cACH,QAAO;CAGT,MAAM,OAAO,cAAc,aAAa;AACxC,QAAO,gBAAgB,cAAc,gBAAgB,WAAW,OAAO"}