I typically use Modernizr to feature-detect for CSS features like flex-box support and fall back to older methods if unsupported:
.mz-flexbox {
// use flex
}
.mz-noflexbox {
// use floats
}
How would one accomplish something similar with styled-components with something like Modernizr where classes are added to the <html>
element to indicate feature support?
My only thought at this point is importing components in my "App.js" and using injectGlobal
to target them:
import MyComponent from './components/my-component';
injectGlobal`
.mz-flexbox {
${MyComponent} {
// use flex
}
}
`;
It appears @supports
would be a sweet solution, but I do care about Internet Explorer, so this is not a solution for this project.
Lastly, I'm not sure if this is a good way to do this, but would something like this work for feature detection? I'm not 100% on what gets compiled with styled-components and what can be calculated at run-time:
styled.div`
${window.Modernizr.flexbox && css`
display: flex;
align-items: center;
`}
`;
Will that work for detection at run-time?