UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

1 lines 3.32 kB
{"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,QAAQ;CAC9C,IAAI,SACF,OAAO;CAKT,MAAM,cAAc,KAAK,iBAAiB,GAAG;CAC7C,KAAK,IAAI,IAAI,GAAG,IAAI,YAAY,QAAQ,KAAK,GAAG;EAC9C,MAAM,KAAK,YAAY;EAGvB,IAAK,GAAe,YAAY;GAC9B,MAAM,gBAAgB,sBAAyB,UAAW,GAAe,UAAW;GACpF,IAAI,eACF,OAAO;EAEX;CACF;CAEA,OAAO;AACT;AAEA,SAAgB,uBACd,UACA,OAAwC,UACnC;CACL,MAAM,UAAe,CAAC;CAItB,MAAM,WAAW,KAAK,iBAAoB,QAAQ;CAClD,QAAQ,KAAK,GAAG,MAAM,KAAK,QAAQ,CAAC;CAIpC,MAAM,cAAc,KAAK,iBAAiB,GAAG;CAC7C,KAAK,IAAI,IAAI,GAAG,IAAI,YAAY,QAAQ,KAAK,GAAG;EAC9C,MAAM,KAAK,YAAY;EAGvB,IAAK,GAAe,YAAY;GAC9B,MAAM,iBAAiB,uBAA0B,UAAW,GAAe,UAAW;GACtF,QAAQ,KAAK,GAAG,cAAc;EAChC;CACF;CAEA,OAAO;AACT;;;;;AAMA,SAAgB,eACd,eACuB;CACvB,IAAI,CAAC,eACH,OAAO;CAGT,MAAM,OAAO,cAAc,YAAY;CACvC,OAAO,gBAAgB,cAAc,gBAAgB,WAAW,OAAO;AACzE"}