Skip to content
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

useCss hook #1321

Closed
lifeiscontent opened this issue Apr 18, 2019 · 6 comments
Closed

useCss hook #1321

lifeiscontent opened this issue Apr 18, 2019 · 6 comments

Comments

@lifeiscontent
Copy link
Contributor

lifeiscontent commented Apr 18, 2019

@mitchellhamilton hey, I'm not sure if this is possible because I don't know the exact internals of emotion, but from a conceptual level this might make sense.

I'm wondering if we could do something like

const MyComponent = props => {
   const theme = useContext(ThemeContext);
   const className = useCss({color: theme.red});
   return (
     <div className={className}>
       MyComponent
     </div>
    );
}

any thoughts?

from a birds eye view, some thoughts here are, you now don't need to worry about shouldForwardProp
you don't need to interpolate with color: ${props => props.theme.red} and the entire component doesn't need to be wrapped, if the ref needs to be forwarded, then you just use forwardRef, etc.

@FezVrasta
Copy link
Member

The main issue with the React Hooks version of ClassNames (basically, what you are suggesting here), is that the css call needs to render the style tag in the same context where it's used (this is needed for server side rendering compatibility).

React Hooks don't render anything.

A possible solution would be to have the hook find its closest "Emotion" parent (either a styled component, a ClassName component, a ThemeProvider or anything else that has the ability to render in the DOM.

But this may introduce some unexpected behaviors.

@BitPhinix
Copy link

Also useStyle would probably be a better name for the hook 😄

@sbking
Copy link

sbking commented Jun 6, 2019

Would an API like this be possible:

const MyComponent = props => {
  const theme = useContext(ThemeContext);
  const [Stylesheet, classNames] = useStyles({
    foo: { color: theme.red }
  });
  return (
    <>
       <Stylesheet />
       <div className={classNames.foo} />;
    </>
  );
};

@ekaradon
Copy link

ekaradon commented Jun 18, 2019

Following the migration from emotion@9 to emotion@10 with react-toastify, I am also facing an issue here.
I have the following hook:

function useSuccessNotification(valid, message) {
    useEffect(() => {
        if (valid) {
            toast.success(message, {
                className: css({
                    ...
                })
            });
        }
    }, [message, valid]);
}

Far away from any render, So I cannot use the ClassNames render prop tip. A hook would be really nice.

Do you see how I could make it work without using deprecated library?

@Andarist
Copy link
Member

Andarist commented Nov 3, 2019

We'll rethink how this problem can be handled right now. We are working on a v11, so we'd like to address problems like this. The solution is not obvious - so no promises yet, but please know that we are going to think about this.

@Andarist Andarist closed this as completed Nov 3, 2019
@kizu
Copy link

kizu commented Feb 6, 2020

@Andarist Are there any news about this? Did you discuss if you'd like to implement something like that, or didn't yet get a chance?

I'd really like this kind of an API. Even if with limitations — there are runtime-only environments where this could be really nice, and in others I like the proposal with a StyleSheet returned from the hook (though, I would reverse the order: [className, StyleSheet]), or a shared Context could be used as well.

The extra bit of setup would be needed to do only once, while the profit from the DX would be much bigger.

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

No branches or pull requests

7 participants