• Stars
    star
    5,348
  • Rank 7,724 (Top 0.2 %)
  • Language
    JavaScript
  • Created about 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation

Aphrodite npm version Build Status Coverage Status Gitter chat gzip size size

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.

  • Works great with and without React
  • Supports media queries without window.matchMedia
  • Supports pseudo-selectors like :hover, :active, etc. without needing to store hover or active state in components. :visited works just fine too.
  • Supports automatic global @font-face detection and insertion.
  • Respects precedence order when specifying multiple styles
  • Requires no AST transform
  • Injects only the exact styles needed for the render into the DOM.
  • Can be used for server rendering
  • Few dependencies, small (20k, 6k gzipped)
  • No external CSS file generated for inclusion
  • Autoprefixes styles

Installation

Aphrodite is distributed via npm:

npm install --save aphrodite

API

If you'd rather watch introductory videos, you can find them here.

import React, { Component } from 'react';
import { StyleSheet, css } from 'aphrodite';

class App extends Component {
    render() {
        return <div>
            <span className={css(styles.red)}>
                This is red.
            </span>
            <span className={css(styles.hover)}>
                This turns red on hover.
            </span>
            <span className={css(styles.small)}>
                This turns red when the browser is less than 600px width.
            </span>
            <span className={css(styles.red, styles.blue)}>
                This is blue.
            </span>
            <span className={css(styles.blue, styles.small)}>
                This is blue and turns red when the browser is less than
                600px width.
            </span>
        </div>;
    }
}

const styles = StyleSheet.create({
    red: {
        backgroundColor: 'red'
    },

    blue: {
        backgroundColor: 'blue'
    },

    hover: {
        ':hover': {
            backgroundColor: 'red'
        }
    },

    small: {
        '@media (max-width: 600px)': {
            backgroundColor: 'red',
        }
    }
});

Conditionally Applying Styles

Note: If you want to conditionally use styles, that is simply accomplished via:

const className = css(
  shouldBeRed() ? styles.red : styles.blue,
  shouldBeResponsive() && styles.small,
  shouldBeHoverable() && styles.hover
)

<div className={className}>Hi</div>

This is possible because any falsey arguments will be ignored.

Combining Styles

To combine styles, pass multiple styles or arrays of styles into css(). This is common when combining styles from an owner component:

class App extends Component {
    render() {
        return <Marker styles={[styles.large, styles.red]} />;
    }
}

class Marker extends Component {
    render() {
        // css() accepts styles, arrays of styles (including nested arrays),
        // and falsy values including undefined.
        return <div className={css(styles.marker, this.props.styles)} />;
    }
}

const styles = StyleSheet.create({
    red: {
        backgroundColor: 'red'
    },

    large: {
        height: 20,
        width: 20
    },

    marker: {
        backgroundColor: 'blue'
    }
});

Resetting Style Cache

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.

import { reset } from 'aphrodite';

reset();

While the resetInjectedStyle function can be used to reset the injected cache for a single key (usually the class name).

import { resetInjectedStyle } from 'aphrodite';

resetInjectedStyle('class_1sAs8jg');

Server-side rendering

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:

import { StyleSheetServer } from 'aphrodite';

// Contains the generated html, as well as the generated css and some
// rehydration data.
var {html, css} = StyleSheetServer.renderStatic(() => {
    return ReactDOMServer.renderToString(<App/>);
});

// Return the base HTML, which contains your rendered HTML as well as a
// simple rehydration script.
return `
    <html>
        <head>
            <style data-aphrodite>${css.content}</style>
        </head>
        <body>
            <div id='root'>${html}</div>
            <script src="./bundle.js"></script>
            <script>
                StyleSheet.rehydrate(${JSON.stringify(css.renderedClassNames)});
                ReactDOM.render(<App/>, document.getElementById('root'));
            </script>
        </body>
    </html>
`;

Disabling !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:

import { StyleSheet, css } from 'aphrodite/no-important';

Minifying style names

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.

import { StyleSheet, minify } from 'aphrodite';

// Always keep the full style names
minify(false);

// ... proceed to use StyleSheet.create etc.

Font Faces

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.

const coolFont = {
    fontFamily: "CoolFont",
    fontStyle: "normal",
    fontWeight: "normal",
    src: "url('coolfont.woff2') format('woff2')"
};

const styles = StyleSheet.create({
    headingText: {
        fontFamily: coolFont,
        fontSize: 20
    },
    bodyText: {
        fontFamily: [coolFont, "sans-serif"]
        fontSize: 12
    }
});

Aphrodite will ensure that the global @font-face rule for this font is only inserted once, no matter how many times it's referenced.

Animations

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.

const translateKeyframes = {
    '0%': {
        transform: 'translateX(0)',
    },

    '50%': {
        transform: 'translateX(100px)',
    },

    '100%': {
        transform: 'translateX(0)',
    },
};

const opacityKeyframes = {
    'from': {
        opacity: 0,
    },

    'to': {
        opacity: 1,
    }
};

const styles = StyleSheet.create({
    zippyHeader: {
        animationName: [translateKeyframes, opacityKeyframes],
        animationDuration: '3s, 1200ms',
        animationIterationCount: 'infinite',
    },
});

Aphrodite will ensure that @keyframes rules are never duplicated, no matter how many times a given rule is referenced.

Use without React

Aphrodite was built with React in mind but does not depend on React. Here, you can see it used with Web Components:

import { StyleSheet, css } from 'aphrodite';

const styles = StyleSheet.create({
    red: {
        backgroundColor: 'red'
    }
});

class App extends HTMLElement {
    attachedCallback() {
        this.innerHTML = `
            <div class="${css(styles.red)}">
                This is red.
            </div>
        `;
    }
}

document.registerElement('my-app', App);

Caveats

Style injection and buffering

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.

import { StyleSheet, css } from 'aphrodite';

class Component extends React.Component {
    render() {
        return <div ref="root" className={css(styles.div)} />;
    }

    componentDidMount() {
        // At this point styles might not be injected yet.
        this.refs.root.offsetHeight; // 0 or 10

        setTimeout(() => {
            this.refs.root.offsetHeight; // 10
        }, 0);
    }
}

const styles = StyleSheet.create({
    div: {
        height: 10,
    },
});

Assigning a string to a content property for a pseudo-element

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:

const styles = StyleSheet.create({
  large: {
      ':after': {
        content: '"Aphrodite"',
      },
    },
  },
  small: {
      ':before': {
        content: "'Aphrodite'",
      },
    },
  });

The generated css will be:

  .large_im3wl1:after {
      content: "Aphrodite" !important;
  }

  .small_ffd5jf:before {
      content: 'Aphrodite' !important;
  }

Overriding styles

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:

Do this:

const styles = StyleSheet.create({
  foo: {
    color: 'red'
  },

  bar: {
    color: 'blue'
  }
});

// ...

const className = css(styles.foo, styles.bar);

Don't do this:

const styles = StyleSheet.create({
  foo: {
    color: 'red'
  },

  bar: {
    color: 'blue'
  }
});

// ...

const 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:

.foo_im3wl1 {
  color: red;
}
.bar_hxfs3d {
  color: blue;
}

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

.foo_im3wl1 {
  color: red;
}
.bar_hxfs3d {
  color: blue;
}

then you will get the appropriate effect of the bar styles overriding the foo ones, but if the stylesheet looks like

.bar_hxfs3d {
  color: blue;
}
.foo_im3wl1 {
  color: red;
}

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:

.foo_im3wl1-o_O-bar_hxfs3d {
  color: blue;
}

Object key ordering

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

const styles = StyleSheet.create({
    ordered: {
        margin: 0,
        marginLeft: 15,
    },
});
css(styles.ordered);

you might expect the following CSS to be generated:

margin: 0px;
margin-left: 15px;

but depending on the ordering of the keys in the style object, the CSS might appear as

margin-left: 15px;
margin: 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:

  1. 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.

    const styles = StyleSheet.create({
        ordered: {
            marginTop: 0,
            marginRight: 0,
            marginBottom: 0,
            marginLeft: 15,
        },
    });
  2. Specify the ordering of your styles by specifying them using a Map. Since Maps preserve their insertion order, Aphrodite is able to place your styles in the correct order.

    const styles = StyleSheet.create({
        ordered: new Map([
            ["margin", 0],
            ["marginLeft", 15],
        ]),
    });

    Note that Maps are not fully supported in all browsers. It can be polyfilled by using a package like es6-shim.

Advanced: Extensions

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:

// my-aphrodite.js
import {StyleSheet} from "aphrodite";

export default StyleSheet.extend([extension1, extension2]);

// styled.js
import {StyleSheet, css} from "my-aphrodite.js";

const styles = StyleSheet.create({
    ...
});

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.

Creating extensions

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():

const mySelectorHandler = ...;

const myExtension = {selectorHandler: mySelectorHandler};

const { StyleSheet: newStyleSheet, css: newCss } = StyleSheet.extend([myExtension]);

As an example, you could write an extension which generates global styles like

const globalSelectorHandler = (selector, _, generateSubtreeStyles) => {
    if (selector[0] !== "*") {
        return null;
    }

    return generateSubtreeStyles(selector.slice(1));
};

const globalExtension = {selectorHandler: globalSelectorHandler};

This might cause problems when two places try to generate styles for the same global selector however! For example, after

const styles = StyleSheet.create({
    globals: {
        '*div': {
            color: 'red',
        },
    }
});

const styles2 = StyleSheet.create({
    globals: {
        '*div': {
            color: 'blue',
        },
    },
});

css(styles.globals);
css(styles2.globals);

It isn't determinate whether divs will be red or blue.

Minify class names

Minify class names by setting the environment variable process.env.NODE_ENV to the string value production.

Tools

TODO

  • Add JSdoc
  • Consider removing !important from everything.

Other solutions

License (MIT)

Copyright (c) 2016 Khan Academy

More Repositories

1

style-guides

Docs for the Organization
Shell
2,136
star
2

khan-exercises

A (deprecated) framework for building exercises to work with Khan Academy.
HTML
1,594
star
3

perseus

Perseus is Khan Academy's exercise question editor and renderer.
TypeScript
1,368
star
4

genqlient

a truly type-safe Go GraphQL client
Go
1,042
star
5

react-components

JavaScript
1,008
star
6

live-editor

A browser-based live coding environment.
JavaScript
754
star
7

flow-to-ts

Convert flow code to typescript
TypeScript
385
star
8

khan-api

Documentation for (and examples of) using the Khan Academy API
375
star
9

gae_mini_profiler

A ubiquitous mini-profiler for Google App Engine, inspired by mvc-mini-profiler
Python
273
star
10

khan-mobile

Youโ€™re probably looking for www.github.com/khan/mobile
JavaScript
239
star
11

Prototope

Swift library of lightweight interfaces for prototyping, bridged to JS
Swift
230
star
12

math-input

math-input = react + redux + mathquill
JavaScript
219
star
13

snippets

Code related to collecting and pushing weekly snippets
Python
205
star
14

structuredjs

Test JavaScript code, look for functionality.
JavaScript
197
star
15

pull-request-comment-trigger

A github action for detecting a "trigger" in a pull request description or comment
JavaScript
190
star
16

react-multi-select

A multiple select component for React
JavaScript
178
star
17

guacamole

General Use Machine Learning for Learning Library
Python
144
star
18

react-render-server

A node.js server for server-side rendering anything!
JavaScript
140
star
19

tinyquery

A Python in-memory test stub for BigQuery
Python
131
star
20

slicker

a tool for moving things in python
Python
126
star
21

wonder-blocks

React components for Wonder Blocks design system.
TypeScript
123
star
22

KAS

A lightweight JavaScript CAS for comparing expressions and equations.
JavaScript
107
star
23

math-facts

JavaScript
77
star
24

hivemind

Experimental knowledge-management system for Long-term Research references
JavaScript
48
star
25

analytics

Tools to analyze KA logs and other data
Python
45
star
26

kmath

JavaScript Numeric Math Utilities
JavaScript
41
star
27

alertlib

A small library to make it easy to send alerts to various platforms
Python
41
star
28

khan-linter

Lint and code-munging tools for Khan Academy codebase
Python
40
star
29

jenkins-jobs

Scripts and the like that Jenkins jobs can run.
Groovy
35
star
30

react-balance-text

A React wrapper for the Adobe Web Platform's Balance-Text Project
JavaScript
34
star
31

frankenserver

A fork of the Google App Engine SDK with modifications required by Khan Academy
Python
30
star
32

khan-windows

Khan Academy for Windows 8
TypeScript
30
star
33

engblog

KA Engineering blog.
Python
29
star
34

mu-lambda

A small library of functional programming utilities.
JavaScript
25
star
35

gittip-gdoc

Extract records from a Google Doc spreadsheet and bulk set the results on Gittip.
JavaScript
23
star
36

internal-webserver

Code that runs on the khan-academy webserver ec2 instance (for dev tools and the like)
Python
21
star
37

youtube-export

Scripts to download and transcode Khan Academy videos and put them on S3
Python
20
star
38

KhanQuest

Khan Academy the game
JavaScript
18
star
39

culture-cow

NO LONGER USED! This is Culture Cow for HipChat. See Culture Cow code for Slack here: https://github.com/Khan/culture-cron
JavaScript
18
star
40

fuzzy-match-utils

A collection of string matching algorithms designed with React Select in mind.
JavaScript
17
star
41

graphie-to-png

A tool for converting graphie JS code to an image
HTML
17
star
42

react-native-codegen

Generating bindings between js & native via flow types
16
star
43

webapp-i18n-bigfile

The next generation of webapp-i18n, starting in April 2015, that uses git-bigfile to avoid storing large resources.
15
star
44

khan.github.io

An index of some of the open source efforts at Khan Academy
CSS
15
star
45

zendesk-theme

Files for our custom https://khanacademy.zendesk.com/ theme
CSS
15
star
46

typed-context

Sample code for the Khan Academy blog about statically typed context
Go
13
star
47

git-workflow

scripts to enable the git workflow at Khan Academy
Shell
12
star
48

localeplanet

A clone of the l10n files at localeplanet.com.
Python
11
star
49

OhaiPrototope

A prototope bootstrap project in xcode
Swift
11
star
50

dendro

A tool for analyzing dependency trees.
JavaScript
11
star
51

structured-blocks

JavaScript
11
star
52

Early-Math-Prototype-Player

Play the Early Math Prototypes embedded in an iPad app.
Swift
11
star
53

Cantor

Prototypes around a medium and toolset for exploring quantities and arithmetic operations
JavaScript
10
star
54

udp-relay

Fork of Simple UDP proxy at http://aluigi.altervista.org/mytoolz.htm, with modifications required by Khan Academy
C
10
star
55

computing-curriculum

Articles that teach computer programming and computer science on Khan Academy.
HTML
10
star
56

gae-continuous-deploy

A server which polls a repository and automatically deploys to Google App engine
JavaScript
10
star
57

slack-deploy-hooks

Slack outgoing webhooks to power Khan's deployment process
JavaScript
10
star
58

OpenResponses

Prototyping around supporting open-ended responses through peer learning
JavaScript
9
star
59

Early-Math-Prototypes

Exploratory prototypes of interactions for early math
JavaScript
9
star
60

BabyHint

Provide more helpful hints for JavaScript developers.
JavaScript
9
star
61

projects

We aim to provide motivating hands-on learning experiences for students all ages.
Arduino
8
star
62

appengine-mapreduce

A fork of http://code.google.com/p/appengine-mapreduce/ with modifications required by Khan Academy
Python
8
star
63

khan-webhooks

A KA-specific web hook to notify Slack/HipChat about Phabricator and GitHub events.
Python
7
star
64

kotlin-datastore

high-level kotlin library for accessing Google cloud datastore
Kotlin
7
star
65

react-sandbox

Play with your components!
JavaScript
7
star
66

khannotations

A React library for rough, animated, annotations.
TypeScript
7
star
67

KhanAcademy_clr

Khan Academy's colors, accessible via OS X's built-in color picker
7
star
68

BirdAcademy

This experimental learning activity tries to help young children implicitly construct a sense of place value via play.
JavaScript
7
star
69

i18n-babel-plugin

Babel plugin to convert <$_> and <$i18nDoNotTranslate> tags to function calls.
JavaScript
7
star
70

youtube-tools

Miscellaneous tools to modify KA's YouTube videos en masse
Python
6
star
71

translation-assistant

Intelligent translation memory for perseus exercises
JavaScript
6
star
72

wonder-stuff

Packages for sharing features across JavaScript-based projects
TypeScript
6
star
73

beep-boop

Automated issue-frequency HipChat notifier
Python
6
star
74

JSContextBenchmarking

How fast is iOS 7's new web-less JavaScript API?
Swift
6
star
75

todo-tools

Helps you track the TODOs in your codebase
Python
6
star
76

openpyxl

A fork of openpyxl with modifications required by Khan Academy.
Python
6
star
77

ka-clone

manages an isolated local gitconfig for cloned repositories
Python
6
star
78

ActiveQuizTouch

Exploring and expanding on the mechanics and concepts behind Jenova Chen's Active Quiz through multitouch prototyping
JavaScript
6
star
79

khan-i18n

This repo is just used to handle the issues for Khan Academy's internationalization efforts.
5
star
80

declarative-z-indexes

Prevent z-index conflicts by generating them from declarative constraints
JavaScript
5
star
81

two-truths

Two Truths and a Lie Slack Bot
Python
5
star
82

pull-request-workflow-cancel

Conserve resources by cancelling workflow runs for previous commits on a pull-request.
JavaScript
5
star
83

kake

A `make` library in Python
Python
5
star
84

placecomplete

A Select2/jQuery plugin for location autocomplete powered by the Google Maps API
JavaScript
5
star
85

algebra-tool

tool for manipulating algebraic expressions and equations
JavaScript
5
star
86

ka-player

Play your favorite Khan Academy CS programs on your phone
JavaScript
5
star
87

react-build

Configuration for making a custom build of React + ReactART for KA
JavaScript
4
star
88

render-gateway

The core implementation of our render-gateway service
JavaScript
4
star
89

real-time-exercises-dashboard

Dashboard of Khan Academy exercises completed in real time on a map!
JavaScript
4
star
90

khan-mobile-exercises

Edit our CSS to make exercises work great on mobile devices.
CSS
4
star
91

free-response-report

Publication regarding our experiments with open-ended online learning
JavaScript
4
star
92

A11yAnalytics

A tool to help you understand your users' accessibility needs.
Swift
4
star
93

mobile_video_zoom

Utility for producing mobile-friendly video zoom/pan sequences from KA videos
Python
4
star
94

eslint-plugin-khan

eslint plugin with our set of custom rules for various things
JavaScript
4
star
95

pygments-server

A simple server that provides HTTP access to `pygmentize`
Python
4
star
96

tutoring-accuracy-dataset

This repository hosts the paper โ€œLLM Based Math Tutoring: Challenges and Datasetโ€, along with the accompanying dataset. It explores the performance and challenges of Large Language Models (LLMs) in math tutoring scenarios, providing a benchmark dataset for evaluating LLM accuracy in educational contexts.
4
star
97

web-workshop

Go
3
star
98

long-term-research-reports

Repository for developing longer-form reports from the Long-term Research team
HTML
3
star
99

canals

Canals: pure URL routing
JavaScript
3
star
100

fastlike

Go
3
star