FAQ and best practices for adopting v10 new styling library #2387
mannycarrera4
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Why is there a new way of styling for Canvas Kit in v10?
We introduced a new styling library
@workday/canvas-kit-styling
in v10 so that we can slowly move off of Emotion and provide better support to our consumers that need CSS. For a more in depth discussion you can reference our Future of Styling Discussion.How am I supposed to style Canvas Kit components in v10?
At the moment, there should be no change in how you style our components. Meaning, you can still use Emotion styled components, style props, the
css
prop, or inline styles.Should we stop using Style Props, the
css
prop, and Emotion styled components?Short answer: Not yet
Long answer: There's two possible paths:
It's our intention as a library to not dictate how consumers use our library or what preferences they have when styling our components. However with the introduction of this new styling library, and the unstable future of Emotion, we intend to introduce patterns to help guide our consumers when transitioning to this new way of styling.
TLDR
Box
,Flex
,Text
andGrid
will probably be deprecated with the exception of maybe supporting style props that are associated to a token instead of dynamic styles.What does style merging look like in v10?
createStyles
(we'll call this static styling), styles are merged according to CSS specificity. Basically, the order that styles are defined in a file determines merge order. The order of styles in the element's ClassList don't matter.css
prop are used (we'll call these dynamic styling), styles are merged according to the rules of Emotion. The order on the ClassList does matter. Therefore it is best to both ordercreateStyles
correctly in your file as well as the correct order in thecs
prop list.Here's a storybook example covering what style merging looks like.
We wanted to provide backwards compatibility with Emotion while introducing the future of how we want to style components.
How are style props applied differently in v10 than in v9?
Only buttons use our new styling, so the changes only apply to Buttons.
as
prop. This was most commonly a problem with routers using aLink
.Box
has conditional logic to determine how it should do prop filtering. If theas
is a base element (as="button"
,as="a"
), it will use Emotion'sisPropValid
function. If theas
is a component (as={MyComponent}
,as={Link}
), Box would not to any prop filtering. Box cannot assume what props the component understands and therefore theas
component needs to understand all style props as well and filter out manually.mergeStyles
at each layer of wrapping. This means style props aren't unpredictable.Here's more detail on the second point:
It isn't obvious what padding will do. It depends on the implementation of
MyComponent
.Box
works by setting padding at theBox
component rendering layer. Emotion styles components based on a "outermost" styling winning. So thecss
prop added at the render layer will be considered outermost because thepadding
prop is passed via prop drilling to the innermost componentBox
. Style props have a way of making styling merging unpredictable. This can be further complicated ifMyComponent
didn't originally stylepadding
via thecss
prop. Addingpadding
certainly didn't change the external interface of the component, but it changed the runtime contract in a non-obvious way.In v10, we introduced
mergeStyles
which operates the same as thecss
prop from Emotion. Instead of prop drilling to a base component, it applies style props at each component layer.mergeStyles
is also for handling thecs
prop.Beta Was this translation helpful? Give feedback.
All reactions