Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andywer/react-usestyles
π Style components using React hooks. Abstracts the styling library away.
https://github.com/andywer/react-usestyles
css css-in-js jss react react-hooks
Last synced: 2 days ago
JSON representation
π Style components using React hooks. Abstracts the styling library away.
- Host: GitHub
- URL: https://github.com/andywer/react-usestyles
- Owner: andywer
- License: mit
- Created: 2018-11-05T08:58:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-11-20T08:25:41.000Z (3 months ago)
- Last Synced: 2025-02-12T16:15:33.829Z (9 days ago)
- Topics: css, css-in-js, jss, react, react-hooks
- Language: JavaScript
- Homepage:
- Size: 1.31 MB
- Stars: 87
- Watchers: 3
- Forks: 2
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unified React styling hook
Leveraging the [React Hooks API](https://reactjs.org/docs/hooks-intro.html) to provide an elegant, unified styling API.
One simple API based on the hooks API to style all components. Suddenly it does not matter anymore if you are using Emotion, Styled Components or JSS - the styling becomes transparent.
Great for component libraries: Don't force your users into a particular styling library! Style with universal hooks and let the users plug-in the styling library that works best for them.
#### Features
π Style components with `useStyles()`
π‘ Uncouple components from styling library
π Server side rendering
π Theming support out of the box
**Any feedback welcome! Leave [a comment](https://github.com/andywer/react-usestyles/issues/2) or π the repo.**
β οΈΒ Β Attention: Bleeding edge ahead. Don't use this in production.
## Installation
In your app:
```sh
$ npm install react@next react-dom@next @andywer/style-hook @andywer/style-api-jss
```In a component package you only need this:
```sh
$ npm install react@next @andywer/style-hook
```## Live Playgrounds
Here are some code sandboxes to see the style hooks in action. You can also see the source code and live-edit it:
πΈ Basics -
π₯ Material-UI / Simple Hacker News client -## Usage
### `useStyles()`
```jsx
// Button.js
import { useStyles } from "@andywer/style-hook"
import React from "react"export function Button (props) {
const classNames = useStyles({
button: {
padding: "0.6em 1.2em",
background: theme => theme.button.default.background,
color: theme => theme.button.default.textColor,
border: "none",
boxShadow: "0 0 0.5em #b0b0b0",
"&:hover": {
boxShadow: "0 0 0.5em #e0e0e0"
}
},
buttonPrimary: {
background: theme => theme.button.primary.background,
color: theme => theme.button.primary.textColor
}
})const className = `${classNames.button} ${props.primary ? classNames.buttonPrimary : ""}`
return (
{props.children}
)
}
```Rendering your app with style hook support is easy:
Add a provider for the styling library at the top of your app.```jsx
// App.js
import { JssProvider } from "@andywer/style-api-jss"
import React from "react"
import ReactDOM from "react-dom"const App = () => (
{/* ... */}
)ReactDOM.render(, document.getElementById("app"))
```### `useStyle()`
This is a convenience function to define a single CSS class.
```jsx
import { useStyle } from "@andywer/style-hook"
import React from "react"export function Button (props) {
const className = useStyle({
padding: "0.6em 1.2em",
boxShadow: "0 0 0.5em #b0b0b0",
"&:hover": {
boxShadow: "0 0 0.5em #e0e0e0"
}
})
return (
{props.children}
)
}
```### Theming & props
```jsx
import { useStyles } from "@andywer/style-hook"
import React from "react"export function Button (props) {
const classNames = useStyles({
button: {
background: theme => theme.button.default.background,
color: theme => theme.button.default.textColor,
border: () => props.border || "none",
boxShadow: "0 0 0.5em #b0b0b0",
"&:hover": {
boxShadow: "0 0 0.5em #e0e0e0"
}
},
buttonPrimary: {
background: theme => theme.button.primary.background,
color: theme => theme.button.primary.textColor
}
}, [props.border])const className = `${classNames.button} ${props.primary ? classNames.buttonPrimary : ""}`
return (
{props.children}
)
}
```For details see the [`style-hook` package readme](https://github.com/andywer/react-usestyles/blob/master/packages/style-hook/README.md).
## Details
**For more details about the API and usage instructions, check out the [`style-hook` package readme](./packages/style-hook/README.md).**
## API
- [`style-hook`](./packages/style-hook) - The main package providing the hooks
- [`style-api`](./packages/style-api) - Defines the API between styling library provider and hooks (internal)
- [`style-api-jss`](./packages/style-api-jss) - The styling library provider for [JSS](http://cssinjs.org/)## License
MIT