-
Notifications
You must be signed in to change notification settings - Fork 14.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal][style-guide]Tips and best practices for Emotion styled
/css
implementation
#15264
Comments
styled
/css
implementationstyled
/css
implementation
I think this doc needs some "DOs" and "DON'Ts" before we can call it a "coding guideline" - any thoughts on the matter, anyone? |
Great guide @rusackas! Thanks for the hard work. I would like to add some points extracted from CSS prop vs styled to help with the discussion.
For me, one thing that is really important is the typescript checking, and this can be achieved either by using
This really happens in our codebase. We have a lot of warnings of properties that are being passed to styled components without the We also can achieve reusability with the
Since we're using CSS-in-JS all style props are being checked by typescript and we get autocompletion while coding. For me, the difference is bigger when we have conditional styling:
Preferences aside, the real gain is defining the standard, so thanks again for this awesome guide, it will be great content for our wiki 👏🏼 🚀 |
Thoughts on some "rules" to make this more of a style guide. Please feel free to suggest additional ones or contest anything controversial here. DO use DO NOT use |
Added to wiki here |
The following is a proposal to provide guidance on how best to use Emotion, some of the tips/tricks/patterns that exist in the repo, and the pros/cons of using
styled
vscss
approaches, with example use cases. This proposal, after a little clean-up here and feedback from this thread, will be posted to thedev@
email list for lazy consensus before being migrated to the Superset Wiki.Emotion Styling Guidelines
DO use
styled
when you want to include additional (nested) class selectors in your stylesDO use
styled
components when you intend to export a styled component for re-use elsewhereDO use
css
when you want to amend/merge sets of styles compositionallyDO use
css
when you're making a small, or single-use set of styles for a componentDO move your style definitions for the
css
prop to an external variable when they get longDO prefer tagged template literals (
css={css...
) over style objects wherever possible for maximum style portability/consistency (note: typescript support may be diminished, but IDE plugins like this make life easy)DO NOT use
styled
for small, single-use style tweaks that would be easier to read/review if they were inlineDO NOT export incomplete AntD components (make sure all their compound components are exported)
Emotion Tips and Strategies
The first thing to consider when adding styles to an element is how much you think a style might be reusable in other areas of Superset. Always err on the side of reusability here. Nobody wants to chase styling inconsistencies, or try to debug little endless overrides scattered around the codebase. The more we can consolidate, the less will have to be figured out by those who follow. Reduce, reuse, recycle.
When to use
css
orStyled
In short, either works for just about any use case! And you’ll see them used somewhat interchangeably in the existing codebase. But we need a way to weigh it when we encounter the choice, so here’s one way to think about it:
A good use of
styled
syntax if you want to re-use a styled component. In other words, if you wanted to export flavors of a component for use, like so:You can also use
styled
when you’re building a bigger component, and just want to have some custom bits for internal use in your JSX. For example:The
css
prop, in reality, shares all the same styling capabilities asstyled
but it does have some particular use cases that jump out as sensible. For example if you just want to style one element in your component, you could add the styles inline like so:You can also define the styles as a variable, external to your JSX. This is handy if the styles get long and you just want it out of the way. This is also handy if you want to apply the same styles to disparate element types, kind of like you might use a CSS class on varied elements. Here’s a trumped up example:
css
tips and tricksUsing the
theme
css
lets you write actual CSSBy default the
css
prop uses the object syntax with JS style definitions, like so:But you can use the
css
interpolator as well to get away from icky JS styling syntax. Doesn’t this look cleaner?You might say “whatever… I can read and write JS syntax just fine.” Well, that’s great. But… let’s say you’re migrating in some of our legacy LESS styles… now it’s copy/paste! Or if you want to migrate to or from
styled
syntax… also copy/paste!You can combine
css
definitions with array syntaxYou can use multiple groupings of styles with the
css
interpolator, and combine/override them in array syntax, like so:Style variations with props
You can give any component a custom prop, and reference that prop in your component styles, effectively using the prop to turn on a “flavor” of that component
For example, let’s make a styled component that acts as a card. Of course, this could be done with any AntD component, or any component at all. But we’ll do this with a humble
div
to illustrate the pointThen just use the component as
<SuperCard>Some content</SuperCard>
or with the (potentially dynamic) prop:<SuperCard cutout>Some content</SuperCard>
Styled component tips
No need to use
theme
the hard way:It’s very tempting (and commonly done) to use the
theme
prop inline in the template literal like so:Instead, you can make things a little easier to read/type by writing it like so:
Extend an AntD component with custom styling
As mentioned, you want to keep your styling as close to the root of your component system as possible, to minimize repetitive styling/overrides, and err on the side of reusability. In some cases, that means you’ll want to globally tweak one of our core components to match our design system. In Superset, that’s Ant Design (AntD).
AntD uses a cool trick called compound components. For example, the
Menu
component also lets you useMenu.Item
,Menu.SubMenu
,Menu.ItemGroup
, andMenu.Divider
The
Object.assign
trick:Let's say you want to override an AntD component called
Foo
, and haveFoo.Bar
display some custom CSS for theBar
compound component. You can do it effectively like so:You can then import this customized
Foo
and useFoo.Bar
as expected. You should probably save your creation insrc/common/components
for maximum reusability, and add a Storybook entry so future engineers can view your creation, and designers can better understand how it fits the Superset Design System.The text was updated successfully, but these errors were encountered: