Gathering detailed insights and metrics for aphrodite
Gathering detailed insights and metrics for aphrodite
Gathering detailed insights and metrics for aphrodite
Gathering detailed insights and metrics for aphrodite
react-with-styles-interface-aphrodite
react-with-styles interface for Aphrodite
aphrodite-jss
Aphrodite-like API on top of JSS.
jest-aphrodite-react
Jest Snapshot serializer for Aphrodite. Based off the serializer from [jest-glamor-react](https://github.com/kentcdodds/jest-glamor-react).
aesthetic-adapter-aphrodite
Aphrodite support for Aesthetic.
Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation
npm install aphrodite
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5,351 Stars
288 Commits
187 Forks
126 Watching
16 Branches
203 Contributors
Updated on 23 Nov 2024
JavaScript (98.41%)
TypeScript (1.59%)
Cumulative downloads
Total Downloads
Last day
5%
21,839
Compared to previous day
Last week
5%
114,479
Compared to previous week
Last month
12%
444,536
Compared to previous month
Last year
-9.1%
5,060,705
Compared to previous year
3
30
Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation.
Support for colocating your styles with your JavaScript component.
:hover
, :active
, etc. without needing to
store hover or active state in components. :visited
works just fine too.@font-face
detection and insertion.Aphrodite is distributed via npm:
npm install --save aphrodite
If you'd rather watch introductory videos, you can find them here.
1import React, { Component } from 'react'; 2import { StyleSheet, css } from 'aphrodite'; 3 4class App extends Component { 5 render() { 6 return <div> 7 <span className={css(styles.red)}> 8 This is red. 9 </span> 10 <span className={css(styles.hover)}> 11 This turns red on hover. 12 </span> 13 <span className={css(styles.small)}> 14 This turns red when the browser is less than 600px width. 15 </span> 16 <span className={css(styles.red, styles.blue)}> 17 This is blue. 18 </span> 19 <span className={css(styles.blue, styles.small)}> 20 This is blue and turns red when the browser is less than 21 600px width. 22 </span> 23 </div>; 24 } 25} 26 27const styles = StyleSheet.create({ 28 red: { 29 backgroundColor: 'red' 30 }, 31 32 blue: { 33 backgroundColor: 'blue' 34 }, 35 36 hover: { 37 ':hover': { 38 backgroundColor: 'red' 39 } 40 }, 41 42 small: { 43 '@media (max-width: 600px)': { 44 backgroundColor: 'red', 45 } 46 } 47});
Note: If you want to conditionally use styles, that is simply accomplished via:
1const className = css( 2 shouldBeRed() ? styles.red : styles.blue, 3 shouldBeResponsive() && styles.small, 4 shouldBeHoverable() && styles.hover 5) 6 7<div className={className}>Hi</div>
This is possible because any falsey arguments will be ignored.
To combine styles, pass multiple styles or arrays of styles into css()
. This is common when combining styles from an owner component:
1class App extends Component { 2 render() { 3 return <Marker styles={[styles.large, styles.red]} />; 4 } 5} 6 7class Marker extends Component { 8 render() { 9 // css() accepts styles, arrays of styles (including nested arrays), 10 // and falsy values including undefined. 11 return <div className={css(styles.marker, this.props.styles)} />; 12 } 13} 14 15const styles = StyleSheet.create({ 16 red: { 17 backgroundColor: 'red' 18 }, 19 20 large: { 21 height: 20, 22 width: 20 23 }, 24 25 marker: { 26 backgroundColor: 'blue' 27 } 28});
The reset
function can be used to reset the HTML style tag, injection buffer, and injected cache. Useful when Aphrodite needs to be torn down and set back up.
1import { reset } from 'aphrodite'; 2 3reset();
While the resetInjectedStyle
function can be used to reset the injected cache for a single key (usually the class name).
1import { resetInjectedStyle } from 'aphrodite'; 2 3resetInjectedStyle('class_1sAs8jg');
To perform server-side rendering, make a call to StyleSheetServer.renderStatic
, which takes a callback. Do your rendering inside of the callback and return the generated HTML. All of the calls to css()
inside of the callback will be collected and the generated css as well as the generated HTML will be returned.
Rehydrating lets Aphrodite know which styles have already been inserted into the page. If you don't rehydrate, Aphrodite might add duplicate styles to the page.
To perform rehydration, call StyleSheet.rehydrate
with the list of generated class names returned to you by StyleSheetServer.renderStatic
.
Note: If you are using aphrodite/no-important
in your project and you want to render it on server side, be sure to import StyleSheetServer
from aphrodite/no-important
otherwise you are going to get an error.
As an example:
1import { StyleSheetServer } from 'aphrodite'; 2 3// Contains the generated html, as well as the generated css and some 4// rehydration data. 5var {html, css} = StyleSheetServer.renderStatic(() => { 6 return ReactDOMServer.renderToString(<App/>); 7}); 8 9// Return the base HTML, which contains your rendered HTML as well as a 10// simple rehydration script. 11return ` 12 <html> 13 <head> 14 <style data-aphrodite>${css.content}</style> 15 </head> 16 <body> 17 <div id='root'>${html}</div> 18 <script src="./bundle.js"></script> 19 <script> 20 StyleSheet.rehydrate(${JSON.stringify(css.renderedClassNames)}); 21 ReactDOM.render(<App/>, document.getElementById('root')); 22 </script> 23 </body> 24 </html> 25`;
!important
By default, Aphrodite will append !important
to style definitions. This is
intended to make integrating with a pre-existing codebase easier. If you'd like
to avoid this behaviour, then instead of importing aphrodite
, import
aphrodite/no-important
. Otherwise, usage is the same:
1import { StyleSheet, css } from 'aphrodite/no-important';
By default, Aphrodite will minify style names down to their hashes in production
(process.env.NODE_ENV === 'production'
). You can override this behavior by
calling minify
with true
or false
before calling StyleSheet.create
.
This is useful if you want to facilitate debugging in production for example.
1import { StyleSheet, minify } from 'aphrodite'; 2 3// Always keep the full style names 4minify(false); 5 6// ... proceed to use StyleSheet.create etc.
Creating custom font faces is a special case. Typically you need to define a global @font-face
rule. In the case of Aphrodite we only want to insert that rule if it's actually being referenced by a class that's in the page. We've made it so that the fontFamily
property can accept a font-face object (either directly or inside an array). A global @font-face
rule is then generated based on the font definition.
1const coolFont = { 2 fontFamily: "CoolFont", 3 fontStyle: "normal", 4 fontWeight: "normal", 5 src: "url('coolfont.woff2') format('woff2')" 6}; 7 8const styles = StyleSheet.create({ 9 headingText: { 10 fontFamily: coolFont, 11 fontSize: 20 12 }, 13 bodyText: { 14 fontFamily: [coolFont, "sans-serif"] 15 fontSize: 12 16 } 17});
Aphrodite will ensure that the global @font-face
rule for this font is only inserted once, no matter how many times it's referenced.
Similar to Font Faces, Aphrodite supports keyframe animations, but it's treated as a special case. Once we find an instance of the animation being referenced, a global @keyframes
rule is created and appended to the page.
Animations are provided as objects describing the animation, in typical @keyframes
fashion. Using the animationName
property, you can supply a single animation object, or an array of animation objects. Other animation properties like animationDuration
can be provided as strings.
1const translateKeyframes = { 2 '0%': { 3 transform: 'translateX(0)', 4 }, 5 6 '50%': { 7 transform: 'translateX(100px)', 8 }, 9 10 '100%': { 11 transform: 'translateX(0)', 12 }, 13}; 14 15const opacityKeyframes = { 16 'from': { 17 opacity: 0, 18 }, 19 20 'to': { 21 opacity: 1, 22 } 23}; 24 25const styles = StyleSheet.create({ 26 zippyHeader: { 27 animationName: [translateKeyframes, opacityKeyframes], 28 animationDuration: '3s, 1200ms', 29 animationIterationCount: 'infinite', 30 }, 31});
Aphrodite will ensure that @keyframes
rules are never duplicated, no matter how many times a given rule is referenced.
Aphrodite was built with React in mind but does not depend on React. Here, you can see it used with Web Components:
1import { StyleSheet, css } from 'aphrodite'; 2 3const styles = StyleSheet.create({ 4 red: { 5 backgroundColor: 'red' 6 } 7}); 8 9class App extends HTMLElement { 10 attachedCallback() { 11 this.innerHTML = ` 12 <div class="${css(styles.red)}"> 13 This is red. 14 </div> 15 `; 16 } 17} 18 19document.registerElement('my-app', App);
Aphrodite will automatically attempt to create a <style>
tag in the document's <head>
element to put its generated styles in. Aphrodite will only generate one <style>
tag and will add new styles to this over time. If you want to control which style tag Aphrodite uses, create a style tag yourself with the data-aphrodite
attribute and Aphrodite will use that instead of creating one for you.
To speed up injection of styles, Aphrodite will automatically try to buffer writes to this <style>
tag so that minimum number of DOM modifications happen.
Aphrodite uses asap to schedule buffer flushing. If you measure DOM elements' dimensions in componentDidMount
or componentDidUpdate
, you can use setTimeout
or flushToStyleTag
to ensure all styles are injected.
1import { StyleSheet, css } from 'aphrodite'; 2 3class Component extends React.Component { 4 render() { 5 return <div ref="root" className={css(styles.div)} />; 6 } 7 8 componentDidMount() { 9 // At this point styles might not be injected yet. 10 this.refs.root.offsetHeight; // 0 or 10 11 12 setTimeout(() => { 13 this.refs.root.offsetHeight; // 10 14 }, 0); 15 } 16} 17 18const styles = StyleSheet.create({ 19 div: { 20 height: 10, 21 }, 22});
When assigning a string to the content
property it requires double or single quotes in CSS.
Therefore with Aphrodite you also have to provide the quotes within the value string for content
to match how it will be represented in CSS.
As an example:
1const styles = StyleSheet.create({ 2 large: { 3 ':after': { 4 content: '"Aphrodite"', 5 }, 6 }, 7 }, 8 small: { 9 ':before': { 10 content: "'Aphrodite'", 11 }, 12 }, 13 });
The generated css will be:
1 .large_im3wl1:after { 2 content: "Aphrodite" !important; 3 } 4 5 .small_ffd5jf:before { 6 content: 'Aphrodite' !important; 7 }
When combining multiple aphrodite styles, you are strongly recommended to merge all of your styles into a single call to css()
, and should not combine the generated class names that aphrodite outputs (via string concatenation, classnames
, etc.).
For example, if you have a base style of foo
which you are trying to override with bar
:
1const styles = StyleSheet.create({ 2 foo: { 3 color: 'red' 4 }, 5 6 bar: { 7 color: 'blue' 8 } 9}); 10 11// ... 12 13const className = css(styles.foo, styles.bar);
1const styles = StyleSheet.create({ 2 foo: { 3 color: 'red' 4 }, 5 6 bar: { 7 color: 'blue' 8 } 9}); 10 11// ... 12 13const className = css(styles.foo) + " " + css(styles.bar);
Why does it matter? Although the second one will produce a valid class name, it cannot guarantee that the bar
styles will override the foo
ones.
The way the CSS works, it is not the class name that comes last on an element that matters, it is specificity. When we look at the generated CSS though, we find that all of the class names have the same specificity, since they are all a single class name:
1.foo_im3wl1 { 2 color: red; 3}
1.bar_hxfs3d { 2 color: blue; 3}
In the case where the specificity is the same, what matters is the order that the styles appear in the stylesheet. That is, if the generated stylesheet looks like
1.foo_im3wl1 { 2 color: red; 3} 4.bar_hxfs3d { 5 color: blue; 6}
then you will get the appropriate effect of the bar
styles overriding the foo
ones, but if the stylesheet looks like
1.bar_hxfs3d { 2 color: blue; 3} 4.foo_im3wl1 { 5 color: red; 6}
then we end up with the opposite effect, with foo
overriding bar
! The way to solve this is to pass both of the styles into aphrodite's css()
call. Then, it will produce a single class name, like foo_im3wl1-o_O-bar_hxfs3d
, with the correctly overridden styles, thus solving the problem:
1.foo_im3wl1-o_O-bar_hxfs3d { 2 color: blue; 3}
When styles are specified in Aphrodite, the order that they appear in the actual stylesheet depends on the order that keys are retrieved from the objects. This ordering is determined by the JavaScript engine that is being used to render the styles. Sometimes, the order that the styles appear in the stylesheet matter for the semantics of the CSS. For instance, depending on the engine, the styles generated from
1const styles = StyleSheet.create({ 2 ordered: { 3 margin: 0, 4 marginLeft: 15, 5 }, 6}); 7css(styles.ordered);
you might expect the following CSS to be generated:
1margin: 0px; 2margin-left: 15px;
but depending on the ordering of the keys in the style object, the CSS might appear as
1margin-left: 15px; 2margin: 0px;
which is semantically different, because the style which appears later will override the style before it.
This might also manifest as a problem when server-side rendering, if the generated styles appear in a different order on the client and on the server.
If you experience this issue where styles don't appear in the generated CSS in the order that they appear in your objects, there are two solutions:
Don't use shorthand properties. For instance, in the margin example above, by switching from using a shorthand property and a longhand property in the same styles to using only longhand properties, the issue could be avoided.
1const styles = StyleSheet.create({ 2 ordered: { 3 marginTop: 0, 4 marginRight: 0, 5 marginBottom: 0, 6 marginLeft: 15, 7 }, 8});
Specify the ordering of your styles by specifying them using a
Map
.
Since Map
s preserve their insertion order, Aphrodite is able to place your
styles in the correct order.
1const styles = StyleSheet.create({ 2 ordered: new Map([ 3 ["margin", 0], 4 ["marginLeft", 15], 5 ]), 6});
Note that Map
s are not fully supported in all browsers. It can be
polyfilled by using a package
like es6-shim.
Extra features can be added to Aphrodite using extensions.
To add extensions to Aphrodite, call StyleSheet.extend
with the extensions
you are adding. The result will be an object containing the usual exports of
Aphrodite (css
, StyleSheet
, etc.) which will have your extensions included.
For example:
1// my-aphrodite.js 2import {StyleSheet} from "aphrodite"; 3 4export default StyleSheet.extend([extension1, extension2]); 5 6// styled.js 7import {StyleSheet, css} from "my-aphrodite.js"; 8 9const styles = StyleSheet.create({ 10 ... 11});
Note: Using extensions may cause Aphrodite's styles to not work properly. Plain Aphrodite, when used properly, ensures that the correct styles will always be applied to elements. Due to CSS specificity rules, extensions might allow you to generate styles that conflict with each other, causing incorrect styles to be shown. See the global extension below to see what could go wrong.
Currently, there is only one kind of extension available: selector handlers.
These kinds of extensions let you look at the selectors that someone specifies
and generate new selectors based on them. They are used to handle pseudo-styles
and media queries inside of Aphrodite. See the
defaultSelectorHandlers
docs for information about how
to create a selector handler function.
To use your extension, create an object containing a key of the kind of
extension that you created, and pass that into StyleSheet.extend()
:
1const mySelectorHandler = ...; 2 3const myExtension = {selectorHandler: mySelectorHandler}; 4 5const { StyleSheet: newStyleSheet, css: newCss } = StyleSheet.extend([myExtension]);
As an example, you could write an extension which generates global styles like
1const globalSelectorHandler = (selector, _, generateSubtreeStyles) => { 2 if (selector[0] !== "*") { 3 return null; 4 } 5 6 return generateSubtreeStyles(selector.slice(1)); 7}; 8 9const globalExtension = {selectorHandler: globalSelectorHandler};
This might cause problems when two places try to generate styles for the same global selector however! For example, after
1const styles = StyleSheet.create({ 2 globals: { 3 '*div': { 4 color: 'red', 5 }, 6 } 7}); 8 9const styles2 = StyleSheet.create({ 10 globals: { 11 '*div': { 12 color: 'blue', 13 }, 14 }, 15}); 16 17css(styles.globals); 18css(styles2.globals);
It isn't determinate whether divs will be red or blue.
Minify class names by setting the environment variable process.env.NODE_ENV
to the string value production
.
StyleSheet.create
and see the generated CSSCopyright (c) 2016 Khan Academy
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 16/22 approved changesets -- score normalized to 7
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More