-
Notifications
You must be signed in to change notification settings - Fork 621
Hidden component #2301
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
Hidden component #2301
Conversation
🦋 Changeset detectedLatest commit: 685fdb8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
size-limit report 📦
|
docs/content/Hidden.mdx
Outdated
--- | ||
|
||
The `Hidden` component is a `<span />` that will help you hide or show content/components in different viewports. | ||
**Attention:** Use Hidden only to control behaviour in a responsive manner. It does not take any `sx` props. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question, would there be a way to control block vs inline flow for this component? Or is the intention that it should always be inline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does hidden have to render any DOM element? Could it be something like this:
function Hidden({when, children}) {
const isHidden = useResponsiveValue(...)
return !isHidden ? children : null
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah so the tossup was between do a display:none
or not render the component at all. I like not rendering. reduces markup + no need to specify display
value at all.
docs/content/Hidden.mdx
Outdated
type="string or Array" | ||
description="Can be one or more values of 'narrow', 'wide', 'regular'" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonder if a more specific definition would be helpful here:
type="string or Array" | |
description="Can be one or more values of 'narrow', 'wide', 'regular'" | |
type={` | |
| 'narrow' | |
| 'regular' | |
| 'wide' | |
| Array<'narrow' | 'regular' | 'wide'>`} | |
description="The viewport range(s) at which children should be hidden" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I could write the actual size values.
Hi 👋 Should we start this component in drafts till we feel confident about our responsive story? |
Closing this PR. |
After some more thought and trying out the alternative |
9d353c1
to
02c5642
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this component! Left some comments and questions below, let me know if they make no sense 😅 haha
children: React.ReactNode | ||
} | ||
|
||
export const Hidden = ({on: hiddenViewports, children}: HiddenProps) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I loved the when
convention from the docs, it was nice to read through it and think: "Okay, this is hidden when the viewport is narrow and wide". Is there a mental scheme you'd recommend with on
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept going back and forth with when
and on
in my head. Sorry for the mix-up. Vini was also leaning towards when
. Will change to that.
For me its just a shorter sentence to say hidden on narrow viewport
vs hidden when viewport is narrow
. When I think of it, I feel like its a non-native english speaker thing. 🤣
const isNarrowViewport = useMedia(viewportRanges.narrow) | ||
const isRegularViewport = useMedia(viewportRanges.regular) | ||
const isWideViewport = useMedia(viewportRanges.wide) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it'd be possible to generate one media query string from the given viewports? Would be cool to do something like:
function mediaQuery(rangeOrRanges: Array<Viewports> | Viewports) {
const ranges = Array.isArray(rangeOrRanges) ? rangeOrRanges : [rangeOrRanges]
return ranges.map(range => viewportRanges[range]).join(', ')
}
export const Hidden = ({on: hiddenViewports, children}: HiddenProps) => {
const matches = useMedia(mediaQuery(hiddenViewports))
if (matches) {
return null
}
return children
}
Basically any time one of the media queries from on
is true, we know it should hidden, otherwise return the children.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, its a good optimisation!
children: React.ReactNode | ||
} | ||
|
||
export const Hidden = ({on: hiddenViewports, children}: HiddenProps) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we need to worry about providing a default value for SSR in this exploration?
## Example | ||
|
||
```jsx live | ||
<Hidden when="narrow"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the implementation below, will this be changing to on
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's ship it and use it internally to get a better sense of how it feels :)
(the approach I wanted to try with sx doesn't even work yet, so that's my bad!)
The
Hidden
component will be used to hide or show content in a responsive manner. We can do this across three viewport breakpoints aka 'narrow', 'regular' and 'wide'.It is inspired by Braid design system
This was built to satisfy use cases specific to

PageHeader
eg -Here, we want to show different text inside the primary
Button
based on viewport value.Merge checklist