Composition of styles and behavior #2580
NicholasBoll
started this conversation in
General
Replies: 1 comment
-
In the future example, our documentation will know what a stencil is simply by the component. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Problem
We are inconsistent with how we compose styles and behavior. Sometimes we compose behavior in the elemProps hook and sometimes we leave that up to the component. This mixing is partially due to how we styled things using Emotion.
Here's an example:
ComboboxCard
andMenuCard
. I'll simplify components to show render functions and style composition (pretend I'm usingcreate*Component
utilities):It isn't obvious at first glance, but the
ComboboxMenuCard
composes styles and behavior in only the render function.useComboboxMenuCard
does not compose the behavior ofuseMenuCard
in the hook itself. Meaning if someone wanted the behavior ofuseComboboxMenuCard
, they would need to either useMenu.Card
in their render function, or they would need to create a new elemProps hook:This would be required if an application developer wants the behavior without the styling.
Our decision to compose behaviors in the elemProps hook vs the render function is entirely dependent on if we also want the styling.
The way forward
Now we have a reason to separate style and behavior. Our CSS kit will be composed from our React Kit, but will not have behavior. This means we need to be able to compose styling independent of our render functions. To be consistent, we should also compose behavior separately. Stencils allow us to fully separate style and behavior.
In our above example,
const StyledCard = styled(Card)
,StyledCard
has both the styling and behavior ofCard
. Even if we wanted to compose behavior, a styled component links them together. We now have the ability to completely separate style and behavior.The Extensions on Stencils discussion starts to outline extension with stencils. Here's my proposal on moving forward with the Card example.
With this, the composition is always consistent.
elemProps
hooks always compose and stencils always compose. The render function is no longer enforcing the relationship.Future
I'd like to go one step further eventually to make sure there's no composition in the render function:
Beta Was this translation helpful? Give feedback.
All reactions