Skip to content

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

Merged
merged 5 commits into from
Oct 11, 2022
Merged

Hidden component #2301

merged 5 commits into from
Oct 11, 2022

Conversation

pksjce
Copy link
Contributor

@pksjce pksjce commented Sep 2, 2022

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 -
Responsive title and context nav

Here, we want to show different text inside the primary Button based on viewport value.

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

@changeset-bot
Copy link

changeset-bot bot commented Sep 2, 2022

🦋 Changeset detected

Latest commit: 685fdb8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

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

@pksjce pksjce self-assigned this Sep 2, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 2022

size-limit report 📦

Path Size
dist/browser.esm.js 77.21 KB (0%)
dist/browser.umd.js 77.85 KB (0%)

@pksjce pksjce marked this pull request as ready for review September 2, 2022 10:53
@pksjce pksjce requested a review from a team September 2, 2022 10:53
@pksjce pksjce temporarily deployed to github-pages September 2, 2022 11:01 Inactive
---

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.
Copy link
Member

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?

Copy link
Contributor

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
}

Copy link
Contributor Author

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.

Comment on lines 33 to 34
type="string or Array"
description="Can be one or more values of 'narrow', 'wide', 'regular'"
Copy link
Contributor

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:

Suggested change
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"

Copy link
Contributor Author

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.

@siddharthkp
Copy link
Member

Hi 👋

Should we start this component in drafts till we feel confident about our responsive story?

@pksjce
Copy link
Contributor Author

pksjce commented Sep 8, 2022

Closing this PR.
Basically after discussion with @siddharthkp and reviewing Chakra library, I think it's better we use styled system's responsive props to power this hide behaviour. I will update the API docs accordingly.

@pksjce pksjce closed this Sep 8, 2022
@pksjce pksjce reopened this Sep 15, 2022
@pksjce
Copy link
Contributor Author

pksjce commented Sep 15, 2022

After some more thought and trying out the alternative sx way of having responsive css, I'm somewhat gravitating back to the simplicity and utility of this component.
I have addressed existing feedback and moved this component to the drafts folder. I think we should test out the utility before writing off this component.

@pksjce pksjce force-pushed the pk/hidden-component branch from 9d353c1 to 02c5642 Compare September 15, 2022 05:40
@pksjce pksjce temporarily deployed to github-pages September 15, 2022 05:46 Inactive
Copy link
Member

@joshblack joshblack left a 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) => {
Copy link
Member

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?

Copy link
Contributor Author

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. 🤣

Comment on lines +13 to +15
const isNarrowViewport = useMedia(viewportRanges.narrow)
const isRegularViewport = useMedia(viewportRanges.regular)
const isWideViewport = useMedia(viewportRanges.wide)
Copy link
Member

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.

Copy link
Contributor Author

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) => {
Copy link
Member

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">
Copy link
Member

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?

Copy link
Member

@siddharthkp siddharthkp left a 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!)

@pksjce pksjce enabled auto-merge (squash) October 11, 2022 02:03
@pksjce pksjce temporarily deployed to github-pages October 11, 2022 02:08 Inactive
@pksjce pksjce merged commit 45afa3d into main Oct 11, 2022
@pksjce pksjce deleted the pk/hidden-component branch October 11, 2022 02:10
@primer-css primer-css mentioned this pull request Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants