Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@olasearch/styled
Advanced tools
Inspiration - https://github.com/jxnblk/cxs
react-frame-component
document
context and appends style tagdocument
is provided, parent document is selectedfast af css-in-js in 0.7kb
import styled from '@olasearch/styled'
const className = styled('div')((props) => ({
color: props.color
}))
cxs is a minimal css-in-js solution that uses an atomic css approach to maximize performance and deduplication
npm install @olasearch/styled
cxs works with any framework, but this example uses React for demonstration purposes.
import React from 'react'
import cxs from 'cxs'
const Box = (props) => {
return (
<div {...props} className={className} />
)
}
const className = cxs({
padding: '32px',
backgroundColor: 'tomato'
})
export default Box
const className = cxs({
color: 'tomato',
':hover': {
color: 'black'
}
})
const className = cxs({
fontSize: '32px',
'@media screen and (min-width: 40em)': {
fontSize: '48px'
}
})
const className = cxs({
color: 'black',
' > a': {
color: 'tomato'
}
})
For Node.js environments, use the css()
method to return the static CSS string after rendering a view.
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import cxs from 'cxs'
import App from './App'
const html = ReactDOMServer.renderToString(<App />)
const css = cxs.css()
const doc = `<!DOCTYPE html>
<style>${css}</style>
${html}
`
// Reset the cache for the next render
cxs.reset()
Note: cxs does not currently have a mechanism for rehydrating styles on the client, so use with caution in universal JavaScript applications.
cxs also has an alternative higher order component API for creating styled React components, similar to the styled-components API.
import cxs from 'cxs/component'
const Heading = cxs('h1')({
margin: 0,
fontSize: '32px',
lineHeight: 1.25
})
To extend a cxs component, pass it to the component creator function.
const Button = cxs('button')({
color: 'white',
backgroundColor: 'blue'
})
const TomatoButton = cxs(Button)({
backgroundColor: 'tomato'
})
Any component can be passed to cxs to add styles.
import { Link } from 'react-router'
const MyLink = cxs(Link)({
color: 'tomato'
})
Note: components must accept className
as a prop to work with cxs.
cxs components can also handle dynamic styling based on props by passing a function as an argument
const Heading = cxs('h1')(props => ({
color: props.color
}))
To remove style props from the rendered HTML element,
use the prop-types
package to define propTypes
on a component.
cxs/component will remove any prop that matches a key from the propTypes
object.
import cxs from 'cxs/component'
import PropTypes from 'prop-types'
const Heading = cxs('h2')(props => ({
fontSize: props.big ? '48px' : '32px'
}))
Heading.propTypes = {
big: PropTypes.bool
}
Style utility functions, like those in styled-system, can be used with cxs/component.
import cxs from 'cxs/component'
import {
space,
color
} from 'styled-system'
const Heading = cxs('h2')(space, color)
Theming is supported with the <ThemeProvider>
component.
import React from 'react'
import ThemeProvider from 'cxs/ThemeProvider'
import theme from './theme'
const App = props => (
<ThemeProvider theme={theme}>
<Heading>
Hello
</Heading>
</ThemeProvider>
)
import cxs from 'cxs/component'
const Heading = cxs('h2')(props => ({
fontSize: props.theme.fontSizes[4] + 'px',
color: props.theme.blue
}))
cxs(...styles)
Accepts styles objects or functions that return style objects and returns a className string.
cxs.css()
Returns the rendered CSS string for static and server-side rendering.
cxs.reset()
Resets the cache for server-side rendering
cxs/component
A styled-components-like API for creating React components with cxs.
cxs does not handle vendor prefixing to keep the module size at a minimum.
For previous versions of cxs, see the v3 branch or v4 branch
FAQs
Simple CSS in JS style for React
We found that @olasearch/styled demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.