Apply style to Child if parent has class #1188
-
Hi everyone, Sorry if this is a silly question but I was using styled-components and now I have switched to vanilla-extract due to the zero runtime. How I can apply styles to the Child component only if the Parent has a specific class? This is my code: import { style } from "@vanilla-extract/css";
export const Parent = style({
// Parent styles...
)}
export const Child = style({
// Child styles...
selectors: {
[`${Parent}:has(open) &`]: {
// styles that apply to Child, ONLY if Parent has the "open" class.
}
}
}) Thanks in advance! Relevant package versions:
|
Beta Was this translation helpful? Give feedback.
Answered by
mihkeleidast
Sep 20, 2023
Replies: 1 comment 1 reply
-
You are probably looking for the import { style } from "@vanilla-extract/css";
export const Parent = style({
// Parent styles...
})
export const Child = style({
// Child styles...
selectors: {
[`${Parent}:is(.open) &`]: {
// styles that apply to Child, ONLY if Parent has the "open" class.
}
}
}) It should also work without the pseudo selector actually: import { style } from "@vanilla-extract/css";
export const Parent = style({
// Parent styles...
})
export const Child = style({
// Child styles...
selectors: {
[`${Parent}.open &`]: {
// styles that apply to Child, ONLY if Parent has the "open" class.
}
}
}) If the import { style } from "@vanilla-extract/css";
export const Parent = style({
// Parent styles...
})
export const open = style({})
export const Child = style({
// Child styles...
selectors: {
[`${Parent}${open} &`]: {
// styles that apply to Child, ONLY if Parent has the "open" class.
}
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
t-monaco
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are probably looking for the
:is()
pseudo-selector instead of:has()
: https://developer.mozilla.org/en-US/docs/Web/CSS/:isIt should also work without the pseudo selector actually: