I have a styled components component. Within it I use the '> div' selector. However…
- User
I have a styled components component. Within it I use the '> div' selector. However there is another component that I need to find within it and I need to write a selector for it. Within '> div' find me a tbody component and check if it has component tr. if it has tr then make the style border:none but if its not empty then I need to apply styles 'border: 1px solid ${({ theme }) => theme.transparent[10]}; border-radius: 12px; '
- Assistant
You can achieve this by using nested selector in styled-components. Here's an example code snippet:
jsximport styled from 'styled-components'; const StyledComponent = styled.div` > div { tbody { tr { border: ${props => props.children ? '1px solid ${props.theme.transparent[10]}' : 'none'}; border-radius: 12px; } } } `; // Usage <StyledComponent> <div> <tbody> <tr>Content</tr> </tbody> </div> </StyledComponent>In this code snippet, we have a styled component
StyledComponentwith nested selectors to target thetbodyandtrwithin the> div. Depending on whether thetrelement is empty or not, it applies the appropriate border styles.