Last active
April 15, 2023 13:19
-
-
Save oscarmarina/30fc6279fd600bbebc65f6a0bb642947 to your computer and use it in GitHub Desktop.
Testing Support for CSS Pseudo-classes & Pseudo-elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Checks if the given CSS pseudo-selector is supported by the browser. | |
| * | |
| * @param {string} selector - The CSS pseudo-selector to check. | |
| * @returns {boolean} - True if the selector is supported, false otherwise. | |
| */ | |
| export const isPseudoSelectorSupported = (selector) => { | |
| const style = document.createElement('style'); | |
| let isSupported; | |
| try { | |
| document.head.appendChild(style); | |
| style.sheet.insertRule(`${selector} {}`); | |
| isSupported = true; | |
| } catch { | |
| isSupported = false; | |
| } finally { | |
| style.remove(); | |
| } | |
| return isSupported; | |
| }; | |
| /* | |
| console.log(isPseudoSelectorSupported(':focus-visible')); | |
| console.log(isPseudoSelectorSupported('::part(foo)')); | |
| console.log(isPseudoSelectorSupported(':host-context(foo)')); | |
| console.log(isPseudoSelectorSupported(':has(foo)')); | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment