Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanKolnik committed Aug 3, 2023
1 parent 298460b commit 7274efd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/preview/rewriteStyleSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ const warnOnce = (message: string) => {
const isExcludedPseudoElement = (selector: string, pseudoState: string) =>
EXCLUDED_PSEUDO_ELEMENTS.some((element) => selector.endsWith(`${element}:${pseudoState}`))

const rewriteRule = ({ cssText, selectorText }: CSSStyleRule, shadowRoot?: ShadowRoot) => {
const rewriteRule = ({ cssText, selectorText }: CSSStyleRule, { shadowRoot, pseudo }: {
shadowRoot?: ShadowRoot,
pseudo?: any
}) => {


return cssText.replace(
selectorText,
splitSelectors(selectorText)
Expand Down Expand Up @@ -46,7 +51,14 @@ const rewriteRule = ({ cssText, selectorText }: CSSStyleRule, shadowRoot?: Shado
? `:host(${states.map((s) => `.pseudo-${s}`).join("")}) ${plainSelector}`
: `${states.map((s) => `.pseudo-${s}`).join("")} ${plainSelector}`

return [selector, classSelector, ancestorSelector].filter(
const includeAncestor = !pseudo || states.some((state) => pseudo[state] === true)
const selectors = [
selector,
classSelector,
...(includeAncestor ? [ancestorSelector] : [])
]

return selectors.filter(
(selector) => selector && !selector.includes(":not()")
)
})
Expand All @@ -59,21 +71,17 @@ const rewriteRule = ({ cssText, selectorText }: CSSStyleRule, shadowRoot?: Shado
export const rewriteStyleSheet = (
sheet: CSSStyleSheet,
shadowRoot?: ShadowRoot,
shadowHosts?: Set<Element>
shadowHosts?: Set<Element>,
pseudo: any
) => {
// @ts-expect-error
if(sheet.__pseudoStatesRewritten) return
// @ts-expect-error
sheet.__pseudoStatesRewritten = true

try {
let index = -1
for(const cssRule of sheet.cssRules) {
index++
if(!("selectorText" in cssRule)) continue
const styleRule = cssRule as CSSStyleRule
if(matchOne.test(styleRule.selectorText)) {
const newRule = rewriteRule(styleRule, shadowRoot)
const newRule = rewriteRule(styleRule, { shadowRoot, pseudo })
sheet.deleteRule(index)
sheet.insertRule(newRule, index)
if(shadowRoot && shadowHosts) shadowHosts.add(shadowRoot.host)
Expand Down
3 changes: 2 additions & 1 deletion src/preview/withPseudoState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ export const withPseudoState: DecoratorFunction = (

// Rewrite CSS rules for pseudo-states on all stylesheets to add an alternative selector
const rewriteStyleSheets = (shadowRoot?: ShadowRoot) => {
const pseudo = channel.data.storyPrepared[0].parameters.pseudo
let styleSheets = Array.from(shadowRoot ? shadowRoot.styleSheets : document.styleSheets)
if(shadowRoot?.adoptedStyleSheets?.length) styleSheets = shadowRoot.adoptedStyleSheets
styleSheets.forEach((sheet) => rewriteStyleSheet(sheet, shadowRoot, shadowHosts))
styleSheets.forEach((sheet) => rewriteStyleSheet(sheet, shadowRoot, shadowHosts, pseudo))
}

// Only track shadow hosts for the current story
Expand Down
17 changes: 17 additions & 0 deletions stories/Button.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ DirectSelector.parameters = {
active: ["[data-active]"],
},
}

export const DirectSelectorParentDoesNotAffectDescendants = () => (
<>
<Button data-hover>Hovered 1</Button>

<div data-hover>
<Button>Not Hovered 1 </Button>
<Button>Not Hovered 2</Button>
</div>
</>
)

DirectSelectorParentDoesNotAffectDescendants.parameters = {
pseudo: {
hover: ["[data-hover]"],
},
}

0 comments on commit 7274efd

Please sign in to comment.