[labs/react] boolean attributes should allow false to be more react-like
See original GitHub issueDescription
A boolean attribute, by definition, is true when it exists on the element, regardless of the value. There is no hidden="false"
, the false value would be removing the attribute. React allows false values for things like checked
, so it’s confusing to have to do something like hidden={showElement ? undefined : true}
on a boolean attribute.
We have this issue on our Modals.
Steps to Reproduce
set hidden={false}
on a CdsModal and notice that it does not show, as expected.
Live Reproduction Link
https://stackblitz.com/edit/clarity-react-8em3m2?file=src%2FApp.tsx
Expected Results
The hidden
prop can accept a value of false and behave like undefined.
Actual Results
false
is the same as true
Browsers Affected
- Chrome
- Firefox
- Edge
- Safari 11
- Safari 10
- IE 11
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Reactive properties
Lit manages your reactive properties and their corresponding attributes. ... Boolean properties that expose an attribute should default to false. For more ......
Read more >Lit for React Developers
Lit is a simple library for building fast, lightweight web components that work in any framework, or with no framework at all. With...
Read more >How to Avoid the “Boolean Trap” When Designing React ...
But using CSS conventions à la Bootstrap for the API design of your React components can lead to bad decisions. The most famous...
Read more >Uncontrolled Components
To handle this case, you can specify a defaultValue attribute instead of value . ... In HTML, an <input type="file"> lets the user...
Read more >State Management with Apollo Client (Reactive Variables)
Introduction State management is one of the most common problems for ... because React is a library, not a framework), developers have to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Figured it out! It was an async timing issue where I was checking for the hidden attribute before the first render was finished. It passes, looks like our workaround can safely be removed. Thanks for fixing this!
I should note that I only had this issue with the
hidden
attribute on a few components - I didn’t use anything like theid
example you gave above. I also think we had a component with an attribute that took a “true” or “false” or undefined value as 3 separate things, so we should check that one as well. (But again I’m not on that team or at that company anymore, so I’ll give them a nudge)Thanks for this @taylor-vann - I no longer work on the project that was having the issue, but I’ll pass it on to the team. I did clone the repo and checked it out really quick and it most of the jest tests have passed as expected when I remove the higher order component I build to convert false to undefined. One is failing still and I haven’t had a chance to dig into it yet, but it looks good for the most part!