A utility-first CSS-in-JS framework built for React.
npm install @xstyled/styled-components styled-components
Docs
See the documentation at xstyled.dev for more information about using xstyled!
Quicklinks to some of the most-visited pages:
Example
import { x } from '@xstyled/styled-components'
function Example() {
return (
<x.div p={{ _: 3, md: 6 }} bg="white" display="flex" spaceX={4}>
<x.div flexShrink={0}>
<x.img h={12} w={12} src="/img/logo.svg" alt="xstyled Logo" />
</x.div>
<x.div>
<x.h4
fontSize={{ _: 'md', lg: 'xl' }}
fontWeight="medium"
color="black"
>
xstyled
</x.h4>
<x.p color="gray-500">A CSS-in-JS framework built for React.</x.p>
</x.div>
</x.div>
)
}
License
Licensed under the MIT License, Copyright © 2019-present Greg Bergé.
See LICENSE for more information.
Babel Macros - not working
Hi, it would seem I cannot import the macro version of styled-components:
https://www.styled-components.com/docs/tooling#babel-macro
Thought trying also importing from
@xstyled/macro
but no luck.Importing directly from styled-components does work well:
Background:
We have an app based on create-react-app, not ejected, and not using rewire, because CRA supports babel macros, and I wish to utilize that ability to be able to use the
babel-plugin-styled-components
plugin.I need this so my styled-components will be generated with a real display name, to be able to access components in enzyme unit tests.
Related thread
New built-in utility: textSize
🚀 Feature Proposal
I propose a new builtin
textSize
utility, that combines font size and line height.Motivation
I love the inspirations xstyled 2 took from Tailwind, especially the new utility style props. One thing that I'm missing is Tailwind's combination of font size and line height, which makes an awful lot of sense. Unless you have very specific design needs, these two properties are always coupled.
Example
<x.div textSize="xl">...</x.div>
=<x.div fontSize="xl" lineHeight="xl">...</x.div>
Pitch
xstyled 2 already has a lot of builtin utilities and a default theme, which is great! I think taking more inspiration from Tailwind can carve out a niche for xstyled among the dozens of low-level CSS-in-JS libraries by providing a more complete utility-based styling solution.
Any future plans on adding types?
Hey! I've been a long-time user of styled-components and thought I'd give xstyled a try since I could totally relate and understand the motivation behind it!
But regarding the topic of this issue, would it be possible to import styled-components/styled-system's preexisting type library? And if not, are there future plans to add types?
Anyways, I really appreciate your work on this library!
Compatibility with react native
Would be possible to use this project with react-native?. I tried, but I get this error:
scStyled__default.div is not a function
I could see under Box.js it's using a div. (https://github.com/smooth-code/xstyled/blob/master/packages/styled-components/src/Box.js#L4)
Makes sense to create another package to support react-native projects? what do you think? I can try to help
styled.box is different from styled.div`${system}`
🐛 Bug Report
I think these should behave the same way, but they don't:
To Reproduce
https://codesandbox.io/s/funny-brook-cy9ne?file=/src/App.js
Versions
This behaves the same way on 1.x and 2.x so it's not a regression, it's just unexpected
override shouldForwardProp
Summary
Currently, some props are being passed and written to the DOM. Emotion is handling this by using is-prop-valid which checks if the prop is a valid html attribute before writing. Here
Test plan
fix(breakpoints): races in useViewportWidth
Summary
The main fix here is changing the initial state from null to window.innerWidth, so that there isn't a useless transition from (for example)
useDown('md') = true
touseDown('md') = false
when the application loads. Depending on the application, the rerendering penalty can be heavy.While we're here, notice that we can make a couple related changes: First, since the initial state is set properly, we don't need a layout effect, instead we can use a normal effect. Second, swap the order of listener and setWidth to avoid the tiny race between them.
Test plan
This cropped up for me in storybook. I have a component that measures its container to make image layout decisions. On the first load of the app, it would work, but on hot reload, the image would be the wrong size.
The problem is that
useDown('md')
returns the wrong value the first time. It was okay on initial app load because image loading slowed things down enough thatuseDown('md')
would return the right value before the measurement. However on hot reload, when the image is already loaded, the measurement would fire in the window of time between the bogususeDown
and the rightuseDown
.Note that the measurement code also reruns on resize, but since there's no resize occurring, it wouldn't know to remeasure.
Making this change fixed the problem for me.
Add transient Props to @xstyled/emotion
Summary
Emotion allows its consumer apps to change styles based on props. See here. By default, emotion would prevent invalid props from being rendered to the DOM element but since xstyled is overriding the "shouldForwardProp" this functionally is being removed.
Styled-components handle this by prefixing the prop name with a dollar sign $. Transient Props
Do you see any downside or conflict by doing something this? @agriffis @gregberge
Test plan
My previous PR was causing some issues with the "as" so this is why I just remove the ones with the $.
xstyled took a sharp turn into the abomination domain
as a 2-years "veteran" avid user of xstyled, I saw the new website (and new approach) and was SHOCKED.
as someone which writes CSS for about 20 years, I believe it is a complete abomination to write styles as props on the component themselves and not keep pure separation of concerns between components (or HTML elements) and their styles.
I use styled components in a very complex manner and this new approach is far too basic and promotes dirty coding where developers add a mountain of props, all mixed together, some CSS-related and some logic-related, not to mention it's impossible to properly use & read CSS variables (custom properties) this way.
Your choice where to take xstyled has saddened me greatly and I want to express me feelings so you would know that there is at least one user which is believes this is a mistake.
Also, the docs are horrible. Cannot find any real information on
th
utility, or real examples of how it can be used (since I never remember)nor can I find any information regarding extending a an already styled component. The "search" bar of the new website really gives poor results.
th utility returns a function and not a value
(version
1.19.1
)Hi, I'm trying to create automatic CSS variables out of JSX, with a defined theme file:
I have a problem that using
th
orgetColor
shorthand, returns a function, rather than a value:What should be done? Can a value be forced out of it?
Thanks for your kind help!
Generated class names aren't legible
🐛 Bug Report
Component class names are not using display names. They look like this:
To Reproduce
Here are my dependencies:
Expected behavior
Previously without xstyled, I would get this class name in my styled components:
Why height and width are shortened?
I usually do not use shortened props in order to improve readability, and upon looking here and here I wonder why height and width only exists in shortened version.
Incorrect breakpoint with useBreakpoint hook
🐛 Bug Report
According to the documentation, md breakpoint should be returned at
@media (min-width: 768px) { ... }
. Instead it triggers at@media (min-width: 769px) { ... }
To Reproduce
Expected behavior
Link to repl or repo (highly encouraged)
https://codesandbox.io/s/confident-tdd-mhwndm?file=/src/App.js
xstyled v 3.7.0 typescript error
🐛 Bug Report
After updating @xstyled/styled-components and @xstyled/system both to v "3.7.0", following TS issue appeared on all places, where
${system}
is used. In version 3.6.0 everything was fine.To Reproduce
Update to v 3.7.0, as described above and check TS (in. CLI or in IDE)
Expected behavior
No errors.
Actual result
Full dependency list
feat: support react-native
Summary
Hello guys! basically I'm trying to finalize a PR that was adding support for react-native. Can find out a little more about it here: https://github.com/gregberge/xstyled/pull/273, thanks @diegotsi
My idea is basically the same as this PR except that I'll be adding the default use without utility props and the x function with utility props.
Test plan
Working with the latest react-native template:
useUp & useDown accept number values but don't support them
🐛 Bug Report
Hooks
useUp
&useDown
have following type declaration:but calling
useUp(1024)
always returnsfalse
.To Reproduce
Expected behavior
useUp
&useDown
should either disallow number values or handle them properly.Remove redundant styling props in the next major version of xstyled
There are a number of redundant props:
bg
vsbackgroundColor
,maxW
vsmaxWidth
,maxH
vsmaxHeight
On the surface this doesn't seem like a big deal, but if you're making a component library using xstyled which has some base styling and allows overrides by the consumer, this could lead to issues if they don't match using the exact same prop.
We should choose to favor either the verbose or short versions and deprecate+remove the others to reduce complexity.