A hook version of <ClassNames> for power users
See original GitHub issuePreface: I know this topic has already been brought up a few times (#1295, #967, #1321) but I have a use-case that I haven’t seen mentioned before, and a proposed solution that side-steps the inherent complexity of this problem, while giving power-users what they need.
The problem
- Our stack already uses a jsx pragma and so we cannot use Emotion’s.
- At the same time we do server-side rendering with
createCache
. So we don’t need (or want) the functionality of adding sibling<style>
tags. - We really need the ability to convert style objects to class names with just hooks, no render-prop components, because:
- we have hooks that compose hooks that compose hooks, and components have no place there.
- render-prop components mess with the es-lint hooks plugin when a hook is needed inside the render function.
The main issue as I understand it is that most people are going to need the version of Emotion that adds <style>
tags next to the component that rendered them, so a hook is out of the question as it can’t do that (at least not in a clean way). And exposing a hook that’s only useful if you roll your own server-side rendering isn’t ideal…
Proposed solution
So I propose that instead of Emotion supplying users with a potentially confusing hook API, just expose some of the functionality that would be needed to create it (the guts of ClassNames
) and let us power-users roll our own. That way we’re not leading less-informed users astray from the pit of success, but also letting more advanced users integrate Emotion into their existing stack without pain.
I’d be happy to take a stab at a PR that makes the minimal changes necessary to move this problem into ‘user-land’. Is that something you’d be willing to consider merging?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:18
- Comments:5
Top GitHub Comments
Hi @oztune it might (or might not) be interesting to you, but I have written a hook that does this already. For my use-case server side rendering wasn’t needed, but if you are able to get access to an
EmotionCache
on the server it might already work for what you are doing.The
useEmotionCache
hook is specific to our application, and it sounds like you’re already in a position to implement your own version. Because I didn’t need SSR for this hook, my version just exposes the default emotion cache in a custom context:I actually have a similar solution but just for a
cx
function. It’s available here as a codesandbox: https://codesandbox.io/s/solitary-hooks-l8b03?file=/src/useCx.jsI asked in the Emotion Slack workspace whether it’s worth an upstream PR to add this to Emotion’s
@emotion/react
.We have a similar situation in the https://github.com/WordPress/Gutenberg repository:
jsx
pragma@emotion/styled
and we prefer thecss
and hook based approach we’ve already takenFurthermore, we utilize iframes in Gutenberg and need a solution that hooks into the Emotion cache.
We’re in a similar situation here as well:
A TypeScript version of the hook exists here: https://github.com/WordPress/gutenberg/pull/33172/files#diff-f2e01286efb7fd67eb692fb1528160c4450145949d6e4f7daf5b1c6a205453d4
Like I said in Slack, happy to open a PR to add this functionality to
@emotion/react
. I think it’d be a great additional API that expands the use cases for Emotion.Note: My approach, I believe, is rather naïve so I’m hoping for feedback from maintainers about whether this approach is viable and if there are modifications that need to be made. All my research into the Emotion source code says that it should be possible to accomplish things this way and I can’t pinpoint any real issues with what I’ve done but I’d expect there to be at least some performance considerations that I haven’t taken into account.