Replies: 1 comment 1 reply
-
Here's another more composable example where we would directly manipulate the props of child boxes to set their https://codesandbox.io/s/pensive-torvalds-8h4ko7?file=/App.tsx:1688-2522 // these border styles get applied to the immediate children of the `JoinedBox`,
// setting for example the block start borders radiuses of the first child to the `JoinedBox.borderRadius` prop,
// and setting the blockStart/blockEnd borders for the children to the appropriate value,
// based on the index of the child
<JoinedBox border="base" borderRadius="4">
<Box padding="4">
<Text as="h2" variant="headingMd">
Our card title
</Text>
</Box>
{items.map((item) => (
<Box padding="4">
<Text as="span" variant="bodyMd">
{item.title}
</Text>
</Box>
))}
<Box
padding="2"
paddingInlineStart="4"
background="surface-attention"
>
<Text as="span" variant="bodySm">
Our card footer
</Text>
</Box>
<Box padding="2" paddingInlineStart="4" background="surface-warning">
<Text as="span" variant="bodySm">
Another footer
</Text>
</Box>
</JoinedBox> |
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
-
Hello 👋
Our new
Box
component allows us to build elements like this without dipping into custom CSS, nice.Hoping to open some discussion on what approaches we should be taking to build something like this. Traditionally this would be styled using selectors
first-child/last-child
to apply the border selectively and create a dividing line. With theBox
styles applied directly in React props, this can be done with selectively applying the border to the individual line boxes using javascript.Simple enough with basic layouts that might come from an array, we can look at the index. There's a few approaches here:
We can consider each item as it's own
Box
, and selectively apply the border to each side, as well as the border radii to the start and end components, this gets messy quite quickly:Another approach is to have a container
Box
for the outer border, and apply a bottom/top border to eachBox
for the items inside:Much simpler, but if we want to add a header/footer to our "card", we need to take that into account when identifying the index of the items. Not too bad:
But if we are doing something like conditionally rendering the footer, this can also get quite messy.
There is an opportunity here to build a helper component that can do this for us automatically. Here is a scrappy example that takes advantage of
React.Children
. TheJoinedBox
takes the props that a box would normally have, passing them to a containing box, which applies the outer border, then for the inner items I wrap them with aBox
applying the top border with the same styles as the container, and the padding, just for consistent item padding.So my question is, should we have some helper like this, and is this a good approach, or should we be building these layouts manually each time?
Codesandbox with examples
Beta Was this translation helpful? Give feedback.
All reactions