chantastic.fm

9: Emotion with Kye Hohenberger

Episode 9 · May 15, 2018 · 00:56:19

Hosted by Michael Jackson · With Kye Hohenberger

Download MP3 · Jump to transcript

Kye Hohenberger is the author of the Emotion JavaScript library, a popular choice among React developers who prefer using CSS-in-JS to traditional CSS stylesheets. In this episode we discuss his work on Emotion including where he got the initial inspiration for the project and his motivation for creating it. We also discuss the future of the project and what may be in store for the future of CSS-in-JS.

Featuring

Links

Transcript

[00:00:06] Michael Jackson: This episode of the React Podcast is brought to you by reacttraining.com. In-person, hands-on training for development teams from React community leaders and experts. Visit reacttraining.com to learn more. Hello, everybody, and welcome back to another exciting episode of the React Podcast. I am your host, Michael Jackson, and I am I am here with a very special guest today, Mr. Kai Hohenberger.

[00:00:35] Kye Hohenberger: Hey, everyone. How you doing?

[00:00:36] Michael Jackson: Thank you for joining us on the React Podcast today, Kai. Kai is joining us from Boulder, Colorado. He is the author of the popular Emotion library for doing CSS in JavaScript that is a pretty popular choice for doing uh you know that kind of thing with uh with React code. And uh super happy that he's able to join us today. Kai, could you could you tell us maybe a little bit about uh you know some of your past experience uh with React? And uh you know how how how we kind of uh got to this point. I I I know that when I was looking for people to be on the podcast, even though I've never met you in person, uh a lot of people were saying, "Kai, Kai, you gotta get this guy on here. Uh you gotta get him on there to talk about uh about emotions." So maybe uh you know tell us a little bit about kind of your background and and what you've been uh working on uh up to this point with uh with React.

[00:01:27] Kye Hohenberger: It started— uh I was doing a side way with uh Aaron Hammer called Sideway, and uh a startup. And uh we decided to go with React, and I just built the whole front end. I was the only front end developer, so I had to learn React and use it on uh and learn uh trial by fire. And I ended up using it, and uh I was using Sass or LESS with it originally, and I got tired of uh compiling the Sass. And so I was like, I'm using Aphrodite. I'm just tired of waiting all the time for the build and all this stuff. I started using Aphrodite, and it was uh one of the original CSS-in-JS libraries, and I kind of just fell in love. And from there, I was using that, and I used it up until we shut Sideway down. And after that, I got into a contracting gig with uh healthline.com, and they were going to rebuild their frontend using modern React, uh server-side rendering, and all this new stuff. And so uh we set that all up, and I Got it all going, we were using Next, and we were using Glamorous. Um, it looked really great, I liked object styles. But as we were using it, we were noticing performance regressions and things that we figured out we couldn't really control. Because of the way the caching works with CSS-in-JS, especially with object styles in Glamour, um, using a styled component, you just can't cache it, so every time it's It's just an expensive operation at a scale of hundreds and hundreds of components. So I took a step back, I tried to uh put some PRs in for Glamorous, and we we we got some performance improvements. Um we were making progress, but it just wasn't enough. And I saw that Sunil, uh he goes by 3.1 on Twitter, he had created this library called Glam. It was very interesting to me because it was very, very fast. So uh I got digging into it, and after a while, I started contributing on it and contributing and cleaning it up. And uh I I like to describe it as almost like a hot rod. It was just- there was no edge cases covered. It was just performance, and it just worked, and it was great. But we started to use it on this on this big project called Healthline, and he gave me his blessing, and I started Emotion as a fork of that. And we just kept iterating and covering corner cases and things that uh it just didn't cover. And we added a lot of features as we went along. And that's where we are now.

[00:03:53] Michael Jackson: I love the tagline for GLAM. It says, "GLAM is a work in progress. It's like giving a glass of ice water to somebody in hell." Uh, that that must have been kind of how you felt, right? I mean, you you you were you were dealing with some pretty tough issues. You said you were you were trying to make uh some uh some some performance improvements to uh Glamorous. Uh, and then you and then you came across this thing. What was so nice about uh GLAM that— uh so so so let me just be clear. Uh you said you were working with Aaron Hammer uh at Sideway. This is the same Aaron Hammer that, uh you know, OAuth Aaron Hammer?

[00:04:33] Kye Hohenberger: Yes.

[00:04:33] Michael Jackson: Yeah, right.

[00:04:34] Kye Hohenberger: I got very fortunate to be able to work with him and have him as mentor.

[00:04:38] Michael Jackson: Yeah, I've never actually uh I've never actually met Aaron, but I everything he's like says, I always seem like I would really like him. So that's that's that's pretty cool that you got to work with him. And so were you were you doing— you know you said you went from Aphrodite to Glamorous. Uh you know were you doing this stuff at Sideway or were you doing it kind of at the next uh the next thing that you were working on?

[00:04:59] Kye Hohenberger: Uh it was towards the end of Sideway that I started really getting into like looking at CSS-in-JS as a way to get away from just styling in another stylesheet. I just really liked the idea of declarative styles. As I hit performance bottlenecks and you know the things that people warned about, uh I just tried to solve them. I didn't want to give up.

[00:05:16] Michael Jackson: Yeah, I get it. So so what uh specifically were you- so you're talking about, you know, building- I'm just trying to get a feel for the kind of thing that you were building with it. You talked about, you know, we're building uh you know hundreds or thousands of components. Um what what is this, you know, kind of the size of the project that that you were working on, or what's the scale that you were working at? Anything anything that you can do to kind of help us understand that?

[00:05:39] Kye Hohenberger: This is kind of an interesting story because Sideway was built as a chat app that was very live. Um, all the data updates were live, so renders were very expensive and they would happen a lot. So you had to like really be- it's all about performance at a- as the app is live updating. When I got to Healthline, I had to change my mentality completely because our goal was to get 100 million viewers a month on a Next application. We started at a very small percentage And I just realized it wasn't gonna work. What we were doing wasn't gonna work. Uh, we just needed this new scale and this new way to doing things. Part of it is going from live updating components and having dynamically changing things as as the the live updates come in. And then you go to this other problem of you gotta do this at scale. And so you can't really use— uh, any minor performance problem just exacerbates itself. When you're running it that hard uh at these massive like health line scale. Does that make sense?

[00:06:43] Michael Jackson: Absolutely. Yeah, I've heard a very similar uh kind of story from uh Nicolas Gallagher, uh who was a a guest on our show just a couple weeks back. He built uh React Native Web. And uh and and he's he's kind of hinted that uh that they ran into to similar issues. Um you mentioned that you were using Next, though. Next has Oh, what is it called? It's not it's not JSX style. I think it's called styled JSX, right? Where you put these sort of styles in between backticks in your, or like right there in your JSX. Have you have you ever-

[00:07:19] Kye Hohenberger: That's correct.

[00:07:19] Michael Jackson: Was that available at the time that you were you know building out Healthline and and and using it? 'Cause I think it it it may uh it may not predate the work that you did on Emotion. I'm not sure though.

[00:07:32] Kye Hohenberger: I think it does. It was available. I I I just didn't- it must have been a personal preference thing. I just didn't like- it didn't seem like it was getting where I was wanting to go. Like, it's just moving your stylesheet into the next to the component, and it's still not as declarative as I'd like. And I liked object styles a lot.

[00:07:51] Michael Jackson: So then comes uh comes Glam. You said uh Sunil gives you his blessing and he says, hey, let's uh why why don't you go ahead and fork this and and run with it. Glam was kind of like a pared-down version of uh work that Sunil had previously done on on Glamour. And you just ran with it. And so so Emotion is basically um CSS-in-JS uh that many people have seen before in this kind of style where you have a a CSS prop essentially to your components, right? And then you pass it an object of CSS styles. And uh and that's the style that you prefer. Could you kind of help us understand why you'd prefer that as opposed to um What are some of the benefits, I guess, of of this approach of doing things? Because honestly, I can probably predict a few of the things that you might be about to say, because I I've talked to a lot of people about CSS-in-JS before. I'm I'm personally kind of on the fence about it still. So I need you to I need you to kind of work with me here and and try and convince me and maybe other people out there who are slow like me and maybe they're a little skeptical. Um, you know, what's what's the major kind of thing that you really like about it?

[00:09:06] Kye Hohenberger: I would start by saying that the reason why I like Emotion is Emotion allows you to use all of these different strategies that you've seen together performantly. I don't like being uh so dogmatic about you should use one style or you should use the other style of CSS-in-JS. I'm more there's like a situation for each little part of it or API, and you can leverage that to your advantage. For instance, let's say you're making a button that you're gonna use all over your app. You might want to use that as a styled component. But at at the level, let's say you want a a one-off button that's needs a margin to the right, whatever, that's when I would use the CSS prop. If I have a kind of complex component but it's one-off, I would probably just do all the styles inline or like React Native into a uh const styles equals in an object, and then just do a bunch of keys and then CSS calls for the values. Whatever you're comfortable with or what works in that situation is what you should use. And the good part about Emotion is that all of these things are using the same underlying APIs and sharing the same cache. So you can intermix them all, and there's no performance hit. There's no- There are performance things that we can talk about in a little bit, but for the most part, you're not gonna see, there's no difference. It's just use what you're comfortable with. And I think that's what we have as an advantage over other libraries that are specifically focused on one kind of way to use CSS-in-JS.

[00:10:34] Michael Jackson: Yeah, I can I can buy that. So you've got a couple of different strategies. Let just to enumerate them as I understand them, one is is kind of the styled-components approach, right? Which uh essentially, uh with that approach, you you basically create a new class, a new component, And and it's it's basically that class's job to encapsulate these styles and render you know a certain tag. Uh and so you have that you know like a button tag, for example. So you have that ability in uh in Emotion to basically just copy and paste a bunch of CSS uh into your uh into your Emotion code and and use that style. You've also got the ability to use— I'm gonna call it like the JSX style approach. Or or or maybe maybe you don't. But but that was something that Glamorous had, right? Where you had like um block and inline-block sort of components. And then you would pass um everything, uh every every style as a prop to the component. Or or or is that not something that's in uh that's in Emotion?

[00:11:39] Kye Hohenberger: That is not in Emotion.

[00:11:41] Michael Jackson: Okay. So the other thing that you've got then is the CSS prop. Which is more like Glamour uh was doing. And and then you basically just merge the objects yourself and and then and then uh you know apply those styles through the CSS prop. And then the other thing that you were talking about is more of kind of like a React Native style, right, where I'm saying, okay, I've got the you know this style object, sort of maybe it's statically defined up at the top, bunch of keys. These these are kind of like my my class names, essentially. Now, when I instantiate one of those style objects, am I passing that thing to like a, you know, emotion dot, you know, create stylesheet create kind of a method, or or is it literally just a plain object?

[00:12:23] Kye Hohenberger: You don't have to do a stylesheet.create. Uh, I would suggest doing the keys would be a CSS call with your styles inside them. And then what you would end up is it literally is just a mapping of a key to a class name, because once that CSS call happens, it returns a string class name.

[00:12:38] Michael Jackson: Got it. Your API is is a CSS a function basically called CSS. Whenever you call that, you pass it an object of these, you know, JavaScript styles, and then you get back a className, something that you can pass to a div className prop.

[00:12:52] Kye Hohenberger: That's correct. And you can also use string styles if that's your thing. A lot of these new editors and like uh formatters support it, and it's actually really neat just to watch Prettier take some string styles and work with them.

[00:13:03] Michael Jackson: Yeah, you know, it's kind of a cool thing. Like I I I didn't really get why people would want to use kind of the styled-components approach, which is essentially just, you know, a big string of CSS. And and and and to be quite honest, I don't do a ton of CSS. And then one of my friends, uh one of my really good friends who's a who's a designer, he's like, yeah, but look how easy this is, right? As I'm styling this stuff uh down here on the console, uh you know, in in the browser, maybe he's maybe he's styling some stuff and he's tweaking some styles, and then he just copies and pastes that CSS. Oh, that's cool. Uh right into his styled component, right? He was explaining to me why why he likes styled components. So um so your CSS function can take either an object literal of key values, like the uh you know the the camel-cased uh CSS style prop names, or you can take one of those strings. Is that right?

[00:13:50] Kye Hohenberger: That's right.

[00:13:51] Michael Jackson: That's awesome that you're you're so flexible, right? You've got like all these different APIs and then there uh you know it's just like do it however you want. And then on the backend, you know everywhere that we're generating a class name, we're gonna be able to share that no matter which API you're using on the on the front.

[00:14:06] Kye Hohenberger: That's correct. I mean, this even goes even further with the Babel plugin, uh because uh for Emotion you're required to have the Babel plugin currently to use the CSS prop. But we're using the Babel plugin adds a lot more features that are really neat. Source maps, hoisting, just like Glam, where you can— uh it'll automatically hoist any pure objects up so they're not— they're this it's uh reusing the same cache over and over again. A lot of performance stuff going on.

[00:14:32] Michael Jackson: Well, I was gonna say, I actually saw some uh some similar stuff done by, was it Dominic Gannaway, I think, uh uh or maybe it wasn't him, but anyway, they were they were actually like hoisting up like statically defined um elements. I'm sure you've seen this as well. And that was a that was a Babel plugin, right? So that you you would essentially just create the element once and then reuse that exact same element in future render calls.

[00:14:57] Kye Hohenberger: That's correct. We basically are all stealing ideas.

[00:14:59] Michael Jackson: Yeah, you're all sharing ideas. And you're yeah, so so you're doing you're taking that exact same approach and applying it you know with your Babel plugin, with Emotion's Babel plugin to uh to the style object, which is awesome. So so tell me a little bit about then the the project itself. It uh it looks like like I said, like I've I've uh I've actually heard uh great things about it from a lot of people uh who are doing this kind of thing, including uh Sunil himself, who's who says, hey, you should really check out Emotion. Um it seems like the project has got a lot of, you know, good support and good traction. It's very active uh on GitHub. You're all let's see, the current version is version 9. It looks like 9.1.3. And uh and version 10 is kind of on the uh on the horizon in the Emotion.js-next repo. Are you are you working on that at all, or is that being done by, you know, other sort of contributors who are helping out?

[00:15:56] Kye Hohenberger: So it's being done by the- I call him the man. Uh, his name is Mitchell. Uh, he's been helping me with the motion from pretty much almost day one. He's uh been a- he's like a fellow owner of the repo. He's uh, he's uh, he's been working on it a lot lately. Basically, I like to think of it that Sunil came up with these wild, amazing ideas. I was crazy enough to work on them. And Mitchell was crazy enough to fix my work. And so, and uh he kind of keeps me in check uh and with reality. But he's been working on this Emotion Next. It's based off Sunil's uh latest work when he was toying with Glam again. It works a little bit different.

[00:16:37] Michael Jackson: Well, I was gonna say, we you know we just talked about how the Babel plugin gives us a lot of stuff in Emotion 9. And in this Emotion Next repo, one of the things uh that Mitchell tells us is, hey, There is no Babel plugin, uh just Babel macros. Can you talk to that uh at all?

[00:16:55] Kye Hohenberger: I think the the key thing is you'll be able to just, instead of like a blanket coverage of just, here's a plugin, you have to do some options, you have to set all this up, you just add Babel macros to your Babelrc. And then anytime you import any of these things, you automatically get all the benefits of the build that's going on with Babel behind the scenes, and you don't even have to think about it.

[00:17:17] Michael Jackson: So so in your actual module, you can make the choice basically whether or not you're gonna opt in and use uh Emotion with this approach. Is that right? Since you're using the macro instead of using the plugin, which is sort of at a global, you know, build level, uh the Babel macro kind of lets you opt in at uh at like a module level. Is that right?

[00:17:38] Kye Hohenberger: Yes. So if you wanted to use the Babel plugin just for like the style, the Babel macro will handle all the styled optimizations, for instance.

[00:17:47] Michael Jackson: So that's pretty cool. Is that something that you think might be the actual- you know, is it- I guess my question is, how experimental is that? Is that something that that seems to be working out? Are people actually using this yet, or is it or is it still just kind of like, maybe this will work?

[00:18:03] Kye Hohenberger: I would say that people are using it. I don't think that it's a a popular thing. Um, in like the general build world, um, or development world. But I mean, we're we use it for— uh, Mitchell came up with this way to use it to automatically build regex at build time, and so you don't have to like code the whole regex. Uh, there's all these cool things you can use it for that just— uh, they're just it's just very handy. And it does nothing special in a sense, it's just running Babel selectively. You're just kind of telling Babel when to run or to do it at compile time. So it's not like a, it's not like that wild of an idea. It's very novel and it's very cool. There's no, there's no denying that, but it's not as crazy as an, of an idea as it sounds, if that makes sense. Like it's not way out there, um, risky. It's just different.

[00:18:58] Michael Jackson: Yeah, I think it totally makes sense. Uh, the, you know, and, and it's, you know, the ability to experiment I think is is super important, right? Spin up a separate repo, run some experiments. What if we could do stuff like this?

[00:19:10] Kye Hohenberger: And I think Emotion Next, that repo is not set in stone. I mean, there's a lot of big questions we have to answer. We're still working on it. I don't know how close it is.

[00:19:18] Michael Jackson: Got it. Well, in that case, let's just back up a little bit and let's talk about the current Emotion library. So you mentioned uh that we make, you know, that that you're making some optimizations basically uh under the hood, right? Could you speak a little bit more about that? What kinds of optimizations could developers expect to see when they're actually using Emotion in their apps?

[00:19:40] Kye Hohenberger: Are you speaking more towards the Babel plugin or just uh the the runtime itself and how it's optimized?

[00:19:45] Michael Jackson: Both, actually. But maybe first let's talk about the runtime and then let's talk about what what are the kind of extra goodies that you get when you when you put the Babel plugin in play.

[00:19:53] Kye Hohenberger: Emotion originally was optimized for handling interpolations in a template literal. So, um, you know, you when you a template literal is just a function call and you have an array of strings and the rest of the arguments are your values that you put in the dollar sign curly braces. Originally, what we would do, and Sunil thought of this, was you would just, in order to run that through Stylus at build time, Stylus can't understand like random CSS template literals, so he just put placeholders in their place that look like valid CSS values. And then from there, you can give that to Stylus. Stylus can parse it all at build time, and then you come out with like uh statically generated CSS sheets. Like, it just works. And it uses CSS variables as the placeholders.

[00:20:49] Michael Jackson: Mm-hmm.

[00:20:50] Kye Hohenberger: So when you change the CSS variable value, it just changes that. So the idea is still based around that. We don't use CSS variables right now because of the browser support. We kind of- moved towards um just doing it all at runtime.

[00:21:06] Michael Jackson: Mm-hmm.

[00:21:07] Kye Hohenberger: And we were doing this- the way I was doing this, I was just beating myself up, just coding and coding and coding about optimize- optimizing all these styles and doing it with objects and stuff. And around Emotion 8, uh Mitchell was like, why don't we just use strings? And uh we can optimize like crazy because the caching will be based on strings and not objects and all this stuff. So once that aha moment happened, like, why are we beating ourselves up? We can remove so much code by just changing objects to strings and then just basing everything on strings. So we did that, and then he pointed out that we could use Stylus, which is what styled-components is using to parse the styles at runtime. And I was like, there's no way, it's it's huge. And he's like, no, it's only 3 kilobytes, it's very, very fast. It's basically just a switch statement. Uh, it's pretty neat. And so these things combined, and then to add that on top, we were using the insert uh the CSS uh OM insert rule to directly insert these styles into the memory representation of a style sheet, not writing to the DOM at all. And all of these things combined to make it very, very fast, uh just one little thing after another.

[00:22:22] Michael Jackson: Well, maybe what maybe you could just tell me, what is what is the what is Stylus's job? It says it's a lightweight CSS preprocessor.

[00:22:30] Kye Hohenberger: That's exactly it. It's uh that's what it is. And basically, what we have to use it for, or what it's for, is expanding uh nested styles and auto-prefixing those things that we still need to auto-prefix. There's very few these days, but uh it handles all that for you. It's actually kind of a hard problem. I tried to write my own CSS parser that unnests, uh, because you know how you can like target child selectors in CSS in JS?

[00:22:57] Michael Jackson: Got it.

[00:22:58] Kye Hohenberger: Um, and with Emotion.

[00:23:00] Michael Jackson: Yeah.

[00:23:00] Kye Hohenberger: Well, this will, this actually unnests all those and like writes the individual styles as they would appear in a style sheet.

[00:23:07] Michael Jackson: Got it. So this is your, this is your parser. Stylus is your parser essentially.

[00:23:10] Kye Hohenberger: Yeah. It's, it's very like a very stripped down high performance version of PostCSS with like a couple plugins.

[00:23:16] Michael Jackson: Got it. And that's so that's why you said when Mitchell came along and he's like, why don't we just use strings? Uh, we could just use this Stylus thing. People give us strings and then we, and you're like, ah, that'll never work. And he's like, hey, it's only 3K. Okay. I'm I'm catching up here. I'm I'm I think I'm I'm I'm learning why you said what you said about 5 minutes ago. So thank you for being patient with me.

[00:23:35] Kye Hohenberger: What's funny about this is, uh, in the last few months I was like, I gotta be able to do this. I can do this myself. And ended up trying to write this, and it's on CodeSandbox, and I ended up in the same exact place that uh @thysultan is at. I'm like, oh, I guess he this is the fastest way to do it. Or the um the hard the very hard problem is unnesting media queries.

[00:24:00] Michael Jackson: Mm-hmm.

[00:24:01] Kye Hohenberger: Um uh it's just a it's it's kind of a a difficult problem and requires a little bit of code.

[00:24:08] Michael Jackson: Yeah.

[00:24:08] Kye Hohenberger: You can't really cheat it, um, but it works great. So,

[00:24:11] Michael Jackson: well, it's, it's cool that, uh, that, uh, Fai Sultan, uh, just developed this thing and said, look, it's got a very, very, you know, this is a tiny sort of single purpose thing. You know, there's all this other stuff out there that is, you know, there's more capable, you know, PostCSS where you can, has a whole plugin system. Uh, but all I really need is, is just this. And by doing so, by, by just focusing on a very, very small uh you know problem space, uh he was able to make a lot of optimizations that uh that make it a lot faster than some of the alternatives. So that's pretty cool.

[00:24:44] Kye Hohenberger: What's really neat about this library is it actually powers styled-jsx, styled-components, Emotion, and maybe a few more that I'm not aware of. Um we all work together to get this— uh there's plugins for Stylus, and you can use Stylus plugins with Emotion. Uh, for example, some people use RTL conversions on the fly, things like that. But uh, we all work together with the styled-components team and the Emotion team and Daizultan, and uh, we all use the same insertRule plugin. So, um, we're kind of getting towards a standardization of what it means.

[00:25:23] Michael Jackson: I was just gonna say, as Stylus gets better, uh, they all get better. So you can use Emotion uh without React. Or this is where the Babel plugin comes in, right? You install uh React Emotion and the Babel plugin for Emotion. And now we get to use uh the styled uh function from React Emotion, which is essentially what we were talking about earlier. It's like the styled-components API uh that Emotion uh exports. We've also got um uh we've also we've also got the you know uh the CSS prop uh that the that the Babel plugin gives us. Um is there anything else that the Babel plugin gives us that I'm missing?

[00:26:07] Kye Hohenberger: So I just want to clarify that the the Babel plugin isn't required for styled. Uh

[00:26:11] Michael Jackson: oh,

[00:26:12] Kye Hohenberger: it's only required if you want to do a styled like styled.div at the moment. So like styled.div or styled.p. Um you can use it just fine. I think actually, uh I'm not sure where they're at on the progress of making that not required anymore.

[00:26:26] Michael Jackson: Yeah, thank yeah, absolutely. Thank you for the clarification. So what is styled return then if you don't get a styled.div or a styled.p or whatever?

[00:26:35] Kye Hohenberger: So you would just have to call it with the- like uh you couldn't do like styled.div. You'd have to do styled uh as a function call and put the string div inside. Like the long form.

[00:26:45] Michael Jackson: Got it, got it, got it, got it.

[00:26:47] Kye Hohenberger: We just don't want to include this giant list of objects in code uh if we don't have to.

[00:26:53] Michael Jackson: Of all of the possible things that you could pass in there.

[00:26:55] Kye Hohenberger: Yeah.

[00:26:56] Michael Jackson: Got it.

[00:26:56] Kye Hohenberger: Yeah, so the what the Babel plugin does is it automatically just converts any dot div to the wrapped parentheses like a function call.

[00:27:04] Michael Jackson: Oh, I got it. You know, I actually might use that in something that I was working on. So the Babel plugin just gives you a little bit of a syntax transformation uh when you're using the styled function and lets you say styled dot instead of styled parens. Um and then uh I would imagine that the Babel plugin also gives you the ability to use that that CSS prop. Is that accurate?

[00:27:24] Kye Hohenberger: It does. I I can go through the whole list. Let's see. If you look at the README for the Babel plugin in motion, um I I actually did some good documentation at one time and did a a table. And so, we can go over all the things. Um it allows components as a selector. What this means is uh if you create a styled component, um I I just used this recently because we have like complex input fields at Gremlin. So Um, you'll have like a styled component that's like the container for the whole input, and then you'll have a styled component that's the actual input inside, and you'll have a styled component that's the label. And all of these things go together as one component. Well, if I wanted to style the actual input that's inside this field, I could just do- I could just target um that input by using its- it the component, uh like you import the component and use it where you would use a selector. And when toString is called on that component, it returns its className identifier.

[00:28:21] Michael Jackson: So in this case, when you- so are you talking about where you would say styled and then pass in, you know, for example, the string div? In this case, you would say styled and pass in a reference to the component class there?

[00:28:33] Kye Hohenberger: You can do that, but what I'm speaking more to is, uh, let's say you wanted to- let's say uh, uh, you're creating a form and you need to change- this is a one-off form where all of your all of your styled-components inputs inside need to be 48 pixels tall. You could literally just write like you were writing a CSS selector and you could write and the ampersand and then you could write uh dollar sign and then the the the the uh curly brace and the temp uh the component name, the actual class. And when it gets called toString, it just returns the class name.

[00:29:04] Michael Jackson: Got it. So in in the actual CSS string, you're using like um uh like the the CSS selector API to target elements that are just uh children of that component, just that children of that one-off component.

[00:29:18] Kye Hohenberger: Yeah, so you like uh let's say you make a styled component that's an input and it's called input. You could just import input and use that where you would use any other CSS selector, and you could change the height to 48 pixels. It's uh you can uh like uh you would do a class a class name, like how you would target a class name with a selector. You can just target you could just use the component.

[00:29:43] Michael Jackson: So you're interpolating that component into the into the CSS string?

[00:29:47] Kye Hohenberger: So it returns like an identifier string, a class name that's unique to that component type. So um all even though each component may have like a different class name because they're slightly different styles, they have an additional class name that's like only for that one kind of component. So each component actually has two class names.

[00:30:08] Michael Jackson: Yeah, so you've already named your component, right? So now you can just take that component, that thing that you've already named, and you don't have to think of a separate class name for it. You can just take that component and say, hey, look, everything that's a child of this component, this component right here, we can we and we can target things that are that are children in that that component. That's really, really cool.

[00:30:28] Kye Hohenberger: Yeah, it's it's handy to use. Uh, I don't like using it a lot. It's a great escape hatch, especially for like complex hover. It seems the inputs are the the biggest thing is like, because all the pseudo tags on inputs and uh targeting everything, um it seems to be where it's used the most. It does minification, it'll remove all like the space, uh uh the Babel plugin will remove all the spacing around uh your string styles especially. So uh when they're compiled, uh because we know how styles work, we can remove spaces, whereas like UglifyJS is just gonna leave that whole big string with all the spaces. We go ahead and remove all of that and compact it all down for you. That helps a little bit. Um, we we add a bunch of uh pure flags for Uglify2C, and these special comments will mark the CSS and styled blocks that uh that they're pure and that if you don't use them, they can be removed. So if you create like a bunch of styles and uh this part of your app doesn't use all of them, and you have like dynamic loading or whatever, it'll uh they'll get dead code eliminated. We we can do static extraction still, so you can like render your whole app, and if you have no dynamic styles, you can extract them out to a sys- it'll like render uh it'll create a CSS file next to your component and you and import that for you. Uh so there's no like dynamic or runtime cost at all. I we're probably gonna remove this uh because there's other libraries that do this better. I think, and are more dedicated to this cause than we are. I don't think it's fair.

[00:32:01] Michael Jackson: I was just trying to think like how you might even do that, right? Because Render, the stuff in Render is so dynamic. Um, how would you determine if it's if it's if an object is static or not? Essentially, you'd have to look at all the values of the properties and determine if any of those are expressions that use, you know, local variables in in the in the local scope, either from state or props, right?

[00:32:22] Kye Hohenberger: Yeah. So Babel has a Babel has all these cheat codes that if you like look through the Babel code, you can find. And they have one called uh uh Babel.is— it's like a Node.isPure or path, I can't remember off the top of my head. So you can just do a quick check and it'll it'll look. Um libraries like uh prepack or like some of these advanced— uh I think Kent C. Dodds actually tried to like follow the path and like make sure that like even try harder to go through the imports and make sure that what you're looking at is static, but it's just it's too hard to keep up and you have to make all these assumptions.

[00:32:55] Michael Jackson: Yeah, it seems like you could You could go quite far down that rabbit hole, it seems like, and and never be 100% sure that what you're looking at is actually static, right?

[00:33:05] Kye Hohenberger: Yeah. I mean, prepack is they're trying so hard to do this, and um they're doing a great job, but uh it's a hard problem.

[00:33:12] Michael Jackson: I would love to I would love to get somebody who's been doing a lot of work on prepack uh on the show. Dominic, who I mentioned earlier, has been doing a lot of work with uh with prepack uh on the React team, but it's obviously not specific to React. But I think prepack does have some very interesting implications for React, at least at least according to him. So uh that's definitely an episode that I want to do in the future.

[00:33:34] Kye Hohenberger: There are ways to solve this. I did solve this by accident. Um I was looking for ways to um import other CSS libraries and legacy CSS libraries into the Emotion cache so that whenever you use that class name, You could like compose it with emotion classes on the fly. And I did this by like scanning all the style elements and uh I got all I get all the internal representations of the styles and use the raw styles to mix in with the emotion styles and all this stuff. Uh it's just it's just so much going on that it's I don't think it's worth it. Um you can also use CSS variables, um but you're making you kind of cut off a lot of developers and uh sites that will allow you to use it if you can only use CSS variables. So that's not a very good option. Uh it's just really hard to write dynamic styles, not dynamic styles in React.

[00:34:31] Michael Jackson: So the Babel plugin does uh lots of cool stuff, it seems like, both from the— you know, it sort of gives you a couple of capabilities, and then it also makes uh you know a couple of optimizations for you on the code. side. So that's uh that's really cool. I'm I'm I'm constantly uh amazed at what people are doing with Babel. You know, like uh just take just you know taking a look at the code, doing some static analysis, doing some substitutions. I think it's really, really impressive. And I I'm always inspired to see uh what people are doing with it. So, you know, kudos to you for for figuring out how to sort of get in there and use it to you know, to do the stuff that you guys are doing. I think it's really, really cool.

[00:35:13] Kye Hohenberger: Yeah, I was really, really intimidated by it at first, um, but I read James Kyle's uh Babel Handbook and that helped. And then I started to use ASTExplorer.net quite a bit, and, uh, then I started to think of it more as like uh writing React code. It's like a declarative— just, you're just writing code as like UI or whatever. And once you start working the nodes, it gets a little easier, and it's just a time thing.

[00:35:38] Michael Jackson: You spend some time with it, right? Like that AST Explorer site that you mentioned uh lets you, you know, you just— uh so for those of you who may not be familiar with it or or familiar with uh writing Babel plugins, you basically just punch in some JavaScript code and then it'll show you uh the Babel AST. And so you get like all of the Babel uh node names. So so Babel's gonna you know go through and parse that code and generate an abstract syntax tree. And you can go and poking poking around in the tree and you can see like, oh, here's what all of these things are called so that when you're actually writing So when you're writing your Babel uh plugin, you can you know know which nodes you're looking for and and you know etc. So I I actually uh and then of course James Kyle's work in the in the Babel uh plugin handbook is incredibly helpful as well. I wrote a just a very, very simple Babel plugin uh to do some transformations on Unpackage last year. Um but uh but both of those were extremely helpful resources. So it's anyway, it's really cool to see people digging in and and and doing stuff with with Babel transforms, especially in this space. I think in the in the CSS space where you're using it to like Um, you know, potentially, you know, extract static pieces of the stuff and render, I think is is incredibly helpful, right? Because the the uh the inverse, I guess, approach is being taken by the whole uh like um templates in JavaScript crowd, right? They start with the static essentially, and then try and identify the dynamic bits of it. Um, and I don't know if if you've if you've used anything like uh Ember or Mustache or Handlebars or or even Angular or stuff like that. But they'll start with the static sort of template and then try and identify, okay, like, or or like the work that was done on on Glimmer. As far as I understand, they'll they'll start with the static template and then parse it out and say, okay, the rest of this, you know, most of this is static, this part, this part, this part's dynamic. Um, but we're kind of taking the opposite approach these days in React by saying, uh, you know, we're just gonna assume the whole thing is uh dynamic unless we drop a Babel plug in there. And like you said, we can look and say, okay, is- Is this function pure? Okay, this you know this part is static, this part is static, this part is static, and extract those pieces. Um so we can actually preserve um the fluidity and the functionality of of JavaScript, right, having a real programming language uh in our render methods, uh you know and and JSX, uh but also getting the benefits of like caching this static stuff. It's it's it it kind of feels like we're taking the inverse approach. Is that does that make any sense? I don't know like I said, I don't know if you've. Spent any time looking into any of those uh frameworks where they use the templates, but it it kind of feels like that.

[00:38:14] Kye Hohenberger: That's kind of what we're doing, and I think we kind of hit the limits. I think both sides are hitting their like- we're hitting like walls and overcoming them, and I think we're getting closer and closer to like the limits. I think prepack is pushing the limits of the dynamic and finding the static, and I think you have something like uh CSS Blocks where they're coming from that other approach. And so I think we're still pushing these limits and some cool stuff's coming out of all of this.

[00:38:40] Michael Jackson: Let me ask your opinion because I think you would probably be one of the best people uh to to have an opinion on this subject, but I hear a lot of other people talking about CSS modules as well as as a you know as a solution for you know some of the problems with CSS when they're using it with their React code. Do you have any thoughts about CSS modules or how they play into this discussion?

[00:39:03] Kye Hohenberger: I I don't I don't find anything bad about them. Uh I think if they're working for your team and you're happy, I I don't think you should switch. I mean, they're great. I just don't like them because you still have a couple of problems. You still have the problem of low-level, um uh what do they call it in CSS modules, like mixing class composing. So you're limited in how you compose. And you're also limited, I believe, I don't I I don't want to be mistaken, but originally Emotion used a similar API with a compose. And what you find out is you can't mix the styles where you would like. Sometimes you want them to be at the end, it's like you want to override at the end, and sometimes you want to override at the beginning. Um, so like let's say you uh you're just like want to compose, but you want things to override things in a certain order. to take precedence. Um, with something like Emotion, whatever order you put in uh the CSS call as an array or as arguments or anything, it works just like object.assign, uh where the rightmost overwrites the next and so on and so on. Whereas with something like CSS modules, what you're gonna have is it's gonna apply all these class names. And so, each class name represents its class its styles, and whatever order they are in the style sheet is whatever order they're going to apply.

[00:40:34] Michael Jackson: So, there's a little bit of indirection there, right? I have to go back to the style sheet to find out what order they're gonna be applied.

[00:40:39] Kye Hohenberger: Yes, that's correct. And with Emotion, you don't worry about it because we mix them at that, like I said, like an object assign. If you look at like a a really big composed style, um, we basically leverage how the browser does overriding. So, um, however you think it works in the browser is how it works with Emotion. Like if you if you use a padding-right call after a padding call, um, only the padding-right is overwritten, and it's in the proper order.

[00:41:04] Michael Jackson: Um, let me ask you about— uh, you said, uh, you know, before we started talking, you said that you've been having some experience uh recently converting a legacy Sass codebase to Emotion. Um, I actually, uh, uh, I recently did a a a project with a, uh, you know, a contractor, and uh, and he wanted to use all Sass. Um, and so basically the deliverable that he gave me was HTML and Sass, and I was like, oh no, what am I gonna do with all this all this Sass stuff? Um, it so may I you you I'm actually very interested to hear what you have to say about how it's going for you because that project, that whole thing kind of stalled for me. I'm about halfway through like um you know breaking down this Sass into like uh stuff that I can like actually actually kind of use. Um I just how's how's that going for you? How's the how's the Sass uh you know to Emotion conversion going for you? And maybe maybe some sort of uh approaches that you're taking and and thinking about how to do that conversion.

[00:42:13] Kye Hohenberger: So I just started at Gremlin about a month and a half ago. I wanted to go obviously over to Emotion, uh but they had a lot of their code was in Sass and it was all working, it's in production. So we can't just like go and change it all. I mean, that's just not realistic. That's- I don't want to be that guy. But what we can do is you can start- I'm sure you use like the class names um uh library that mixes class names. The first thing you can do is you can go and you could switch that import for Emotion's cx import or export. And that has the same exact API. Um, it works exactly the same, except it recognizes Emotion classes and treats them special. So it can like it'll automatically separate what Emotion is and what your your legacy class names and put them next to each other. And it knows the difference. Um. And so what you can use that for is you can slowly start to move, once you switch that import, you can slowly start to move these like um conditionals. And you you'll start to see where inline styles work or maybe some a styled component would work better there and you can use some props. Uh there's no there's no rule that says you can't use them at the same time. Uh my what what we've been doing is just slowly like we Um, change the button over, right? So all the buttons are now in motion. We change the inputs over, so now all the inputs are- and so you can kind of start in either direction and work your way. And as you touch things, you can uh update styles as they need to. Sometimes stuff doesn't even need to be touched, uh like resets or globals or um general styles that you like. Those are perfectly fine to load in with the CSS file. And uh when I was converting uh Sideway over from uh Sass to Aphrodite, I ended up having uh an import that I just kept because there's there's like I said, there's no rule. You can still have these legacy styles that are generally global, but you can work your way through and kind of as you're looking at these components, you can see, oh, this is just a styled component. Oh, this is just a collection of 3 styles. I can just do this inline real quick. And um basically my strategy is I just start- I just put them side by side and move them over, look at them, and delete as I go. Uh that's about it. Uh it's like I said, the cx utility that we export is very handy for this uh because because it can mix for you.

[00:44:46] Michael Jackson: Well, it's nice that you can take an API that you already know, right, which is the class names API. You can swap it out for cx from Emotion, and now you're already using Emotion. Um, and you can, you know, it's a small change in the code, but you can immediately ship that and uh, you know, and and and already start seeing some of the benefits uh from Emotion.

[00:45:06] Kye Hohenberger: Yeah, you would see you would see that like uh, Emotion basically just sets aside any non uh cx sets aside any non-Emotion class name and leaves them in order. It then just appends all this Emotion styles that it knows about and that it can recognize as a one um composed new class name. And that goes on the end. And so you can keep using this and uh you can like remove one legacy class name at a time, or uh sometimes you can just remove the whole thing.

[00:45:35] Michael Jackson: Got it. I think the approach that you're taking also uh just in general is uh is actually a very good approach. I think it's an approach that we don't talk about uh enough when we're talking about adopting new technology in in this industry is uh You know, lots of people, uh, you know, when they're adopting new technology, React, for example, or or maybe Emotion, might say, well, you know, it's time to rewrite everything. Um, and and I I love how you said, I don't want to be that guy, because there's, you know, in any decent-sized company, there's always a person in the room who's like, yeah, let's just uh let's just rewrite it. And uh and you know, my whenever that person says, let's rewrite it, I'm always like, nope. Nah, you probably shouldn't. I mean, if your app is already up and running, you should probably not rewrite it. Uh, you're just gonna end up with a new pile of bugs that are the same as the bugs. You know, they're different bugs, but you're still gonna have a bunch of bugs. Um, so I like what I like how, you know, you're thinking about that. Uh, you know, you're doing a real-life conversion yourself and and seeing how, you know, essentially like seeing how easy it is to convert to Emotion. And has any of that any of that work that you've been doing with uh you know converting this this Sass codebase to Emotion, has any of that- I'm sure I'm sure some of that work is at least filtered back into the Emotion uh library itself and kind of informed, you know, if we had this if we had this thing in Emotion, it would be a lot easier for people to convert to it.

[00:47:08] Kye Hohenberger: I think uh we hit most of that uh doing uh Healthline last year. And so what I'm what I'm learning more this this time around with Gremlin is uh is I'm letting— it's more of like a— uh I'm learning how other people see emotion uh and use it. Um and trying to like realize that the real-world problems and uh constraints that everyday users are seeing when they're trying to convert to Emotion. So things that I take for granted all the time that I just know, um, I'm seeing that, oh my goodness, I should just probably document that. It's not just generally known. And that's the biggest thing I'm learning is I'm trying to trying to get all of that together and bring it back to the documentation. I haven't been doing a good job of that, just being busy, but All of this, I think this time around I'm learning is how I need to communicate to others how to convert and the things that you'll run into. And the guys at Gremlin have been great because I just talk their ear off on like my ideas of how you should write styled and stuff. So, uh, uh, yeah, I think the biggest takeaway is just learning this time around, especially is learning how uh devs see Emotion when confronted with it for the first time, because it it is kind of overwhelming.

[00:48:39] Michael Jackson: Absolutely. That I think that's a major thing that I learned uh when we were writing React Router as well, was um you know we're writing this router and then a lot of people showed up on the repo. I mean, we wrote the first version that like worked fairly well for our use cases, and then a lot of people showed up and were like, hey, but Uh, you know, what if I want to- you know, I'll never forget, like, Dan Abramov showed up one day and he's like, "Hey, I've been doing a lot of hot reloading in React." And I'm like, "Yeah, that sounds cool, but like, I don't even know what hot reloading is, whatever." And he's like, "Anyway, I was wondering, you know, if I'm using React Router, how could I like hot reload my routes?" And I just didn't have an answer for him. I was just like, "I don't-." No, because what does that like what does that even mean? What does that even look like when you hot reload? Like do we need to change the URL when that happens? So anyway, you know, people come in and and and they they say things like that and then it kind of, yeah, it informs your API and and you learn like, oh, they need to do this thing that I'm not even doing, that I wasn't even thinking about, you know, which is uh which is cool. Um and I and I feel like it's really helped us to sort of distill the router down to like um, something that can work for, for everybody's use cases.

[00:49:57] Kye Hohenberger: Yeah, I love the new API. Like, people were hating on it.

[00:50:01] Michael Jackson: Awesome, dude, thank you.

[00:50:02] Kye Hohenberger: I'm glad you like it. I was using it so early.

[00:50:04] Michael Jackson: Oh, dude, I want to see it.

[00:50:05] Kye Hohenberger: Oh, I'm using it for really cool stuff now. So yeah, when, uh, when we launch, I'll, uh, I could talk about a little bit. Uh, it's like, uh, dynamic forms and we're dynamically generating routes and links to them, and it's, it's really cool because Uh, it's one little component with like a recursive, like looping down to object and it just generates routes.

[00:50:25] Michael Jackson: Aren't dynamic routes cool? I I I remember there, think that's that's awesome. The uh I I remember a tweet a while back from, uh I think it might've been Kent or Jared Palmer or somebody. I think it might've been Jared Palmer, but anyway, he was like, it it it was it was funny. It was an if statement around a route. And he was he he was basically just like, you know, and and the if statement basically said like something like, if the user is authorized and logged in, then render a route, which is like the auth panel or something, you know. And and and the tweet was something like, um, hey, if you've ever wanted to have like a protected route in your app, you know, that that somebody who's not logged in cannot access. Like, maybe you could use an if statement, which, which, you know, seems so basic uh of a thing. But I I I remember coming back to that tweet and it had like 300 likes on it, and I was just like, oh my gosh. It was one of those moments for me where I was just like, I guess that's something that most people hadn't really considered before, right? Like, it it it was uh it was one of those moments for me, like what you say about, uh you know, emotion, like like. We should probably document that. You know, we should probably put that in the docs so people can see, like, you can put if statements around your routes. Oh my gosh. And then and then once you once you figure that out, then it's like, well, I could generate them in a loop, which is what it sounds like what you're doing, right? Like I could have a loop of stuff and I could just generate a bunch of route components, just like I generate a bunch of LIs or or TRs in a table or something when I'm when I'm rendering one of those, you know, for my data. Um, sorry. Anyway, it's just to to speak to your point, I thought it was, uh, it was it was kind of funny when I saw that tweet and I realized, oh man, we are doing a terrible job at like telling people about this if like this is news to be to people. Um.

[00:52:23] Kye Hohenberger: It's just such a paradigm shift. I mean, you're you're so used to imperatively uh defining everything and then you have to like structure everything after this definition that you've got. Back in another file and just being able to wrap and say, if credentials, uh if not credentials, redirect to login. It's like amazing.

[00:52:45] Michael Jackson: All right, well, we are we are just about out of time, but I did want to ask you one final question, um which was, and this is a question that I ask for everybody who comes on the podcast, which is, uh you know, if there's anything that you are, you know, stuff that you're not specifically working on, but stuff that maybe you see somebody else working on you're pretty excited about it, you think they're doing some cool work, uh maybe you want to give them a shout out or you know also suggestions for uh people that you might to like to see on the show at some point in the future.

[00:53:15] Kye Hohenberger: I'm a big, big fan of styled system and what that means and uh all that that is, uh of the mostly the theme and the idea of um these primitive components Um, being able to be used everywhere. And like this common theme where space is an array, it makes, uh, the values are an array in arrays in these themes. So you can like just move, you can change the value, the index of the array, and like have vertical rhythm. It's just easy. Um, this library's by Brent Jackson. It's, it's a great one. It's called Styled System. Um. Uh, another cool thing we're doing, um, I'm getting big on using uh components for requests. Um, I think you guys are big on this too, um, where you just do like a request and then you have a URL and it does a GET, like a uh request component. Um, I've been really digging into that lately. I think you should have Sunil on uh 3.1. Uh, he'd probably blow your mind with some good ideas.

[00:54:23] Michael Jackson: It's been too long since I've like talked face to face with that guy anyway. I need to I need to catch up with him. I think that's an excellent suggestion. I think a lot of people would enjoy hearing from him too.

[00:54:31] Kye Hohenberger: And I think he has a lot of experience with uh React VR, um if I'm not mistaken. So.

[00:54:37] Michael Jackson: Well, so he's at Facebook these days, right? So uh maybe he's uh maybe he's doing some of the some of the VR stuff with them. Maybe we could ask him about some of that stuff. I mean, React VR is such like a cutting edge such a new thing. I think there are probably like, I could probably count the people on one hand in the world who have experience with with React VR, right? It's-

[00:54:58] Kye Hohenberger: He's been practically begging people to write apps for VR. So I think it's uh it would be a good podcast.

[00:55:04] Michael Jackson: All right. Well, Kai, thank you so much for uh for taking the time to be with us today on the React Podcast. Uh I've really, really enjoyed uh talking to you about the work that you've done on Emotion. And uh and just, you know, like I said, it's it's uh you you you can I can tell just by the you know by the by the stuff that people are saying in the community that you're you're contributing a lot of value to the community. So from one library author to another, thank you for the work that you're doing. Uh it's it's awesome. If you want to follow Kai uh on Twitter, uh he is @TKH44. Uh, on Twitter. Uh, is that your GitHub handle as well?

[00:55:44] Kye Hohenberger: That's everything.

[00:55:45] Michael Jackson: All right, so anywhere, anywhere you see TKH44, that's our man Kai. Um, all right, and thank you so much for, uh, for again, for being with us today, and we will see y'all next week on the React Podcast.

[00:56:00] Kye Hohenberger: Thanks for having me, man. I really appreciate it. I had a great time.