Four approaches to implicit state in compound components, ranked
jesseduffield.com
jesseduffield.com
- https://inclusive-components.design/collapsible-sections/.
Github's elements collection (custom elements/web components) includes several beautifully implemented `details` based approaches to UI problem:
- https://github.com/github/github-elements
Doing this stuff in React is really quite over-complicating it (and making it practically less accessible/reusable). React is uniquely bad for the web among the most popular frameworks:
I find it hard to understand how implementing components within a framework your entire company presumably uses would "[make] it practically less accessible/reusable". I especially take issue with the word "practically", as I can see from some theoretical point of view that an HTML-only component would "enable reuse across all frameworks", but practically speaking I never see people mix frameworks, because that of course would be horrible for performance and real-world reuse.
If we start applying our perceived values to anything more complex, we increase complexity (death via virtue). I’d argue that we should seek out a scaleable virtue system. While some of those cases that were grade A can be proven to be proper in simple components, they become unwieldy in larger component systems if we pursue righteousness like zealots.
Nope. We all suffer together.
But since React is a such a minor part of the JS/TS landscape, who can really blame the TS team for not prioritising this, right!?
But also this example doesn't need to do that? All the complexity is from the contrived requirement that the accordion sections themselves can be arbitrary nodes. If you just said "this has to be text and we're wrapping it in a <p>", there's no need for a component tree here. And besides, it's not like you can just pop anything in there--you'll likely break your layout if you try.
Why is that contrived? I implement complex forms as data input for a workflow runner. Often, there are nested properties only relevant for certain branches of workflows, and to keep the new user's focus and eye, I put these properties within an Accordion. They include classic data inputs as well as tree-browsers for file or other tree structures, arbitrary lists of objects, etc.. It is quite graceful
> it's not like you can just pop anything in there--you'll likely break your layout if you try
And this is where I grow even further confused. It's exactly like you can pop anything in there - otherwise why did I implement an Accordion in the first place? My layout tracks the scrollHeight of the container node holding the children for each section and updates & animates its own size accordingly. It's of course just a flex-column, but maybe you can enlighten me on something I am missing with regards to your comment and this implementation.
And that's why I was saying you have to be careful about what you put in an accordion. It's (usually) not like section 1 is gonna be drastically different than section 8.
I suppose my point, which I'm kind of getting a better grip on now, is that this post isn't so much about "implicit state in compound components" but rather "where does state live when components kind of depend on each other", which like, describes a web app. The compound components/accordion thing is a red herring; anything could go in here: form validation feedback, notifications, etc. etc. etc.
Using the grading framework here, it's probably a C. Looks good, lots of flexibility for the component consumer. Bad TS support, plus the same problems as cloneElement. But I think it's a nifty pattern anyway.