Skip to content

Commit

Permalink
Document generateCSS and generateCSSRuleset methods
Browse files Browse the repository at this point in the history
I'm reluctant to let #61 land because of the complexity it introduces, but would
like these docs to land (slightly modified from #61), and would prefer to not
make them landing dependant on the complexity of #61.
  • Loading branch information
jlfwong committed Jun 7, 2016
1 parent bdb813a commit 29cb180
Showing 1 changed file with 81 additions and 1 deletion.
82 changes: 81 additions & 1 deletion src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,50 @@ import {
objectToPairs, kebabifyStyleName, recursiveMerge, stringifyValue,
importantify, flatten
} from './util';

/**
* Generate CSS for a selector and some styles.
*
* This function handles the media queries, pseudo selectors, and descendant
* styles that can be used in aphrodite styles.
*
* @param {string} selector: A base CSS selector for the styles to be generated
* with.
* @param {Object} styleTypes: A list of properties of the return type of
* StyleSheet.create, e.g. [styles.red, styles.blue].
* @param stringHandlers: See `generateCSSRuleset`
* @param useImportant: See `generateCSSRuleset`
*
* To actually generate the CSS special-construct-less styles are passed to
* `generateCSSRuleset`.
*
* For instance, a call to
*
* generateCSSInner(".foo", {
* color: "red",
* "@media screen": {
* height: 20,
* ":hover": {
* backgroundColor: "black"
* }
* },
* ":active": {
* fontWeight: "bold",
* ">>bar": {
* _names: { "foo_bar": true },
* height: 10,
* }
* }
* });
*
* will make 5 calls to `generateCSSRuleset`:
*
* generateCSSRuleset(".foo", { color: "red" }, ...)
* generateCSSRuleset(".foo:active", { fontWeight: "bold" }, ...)
* generateCSSRuleset(".foo:active .foo_bar", { height: 10 }, ...)
* // These 2 will be wrapped in @media screen {}
* generateCSSRuleset(".foo", { height: 20 }, ...)
* generateCSSRuleset(".foo:hover", { backgroundColor: "black" }, ...)
*/
export const generateCSS = (selector, styleTypes, stringHandlers,
useImportant) => {
const merged = styleTypes.reduce(recursiveMerge);
Expand Down Expand Up @@ -39,6 +82,12 @@ export const generateCSS = (selector, styleTypes, stringHandlers,
);
};

/**
* Helper method of generateCSSRuleset to facilitate custom handling of certain
* CSS properties. Used for e.g. font families.
*
* See generateCSSRuleset for usage and documentation of paramater types.
*/
const runStringHandlers = (declarations, stringHandlers) => {
const result = {};

Expand All @@ -55,6 +104,37 @@ const runStringHandlers = (declarations, stringHandlers) => {
return result;
};

/**
* Generate a CSS ruleset with the selector and containing the declarations.
*
* This function assumes that the given declarations don't contain any special
* children (such as media queries, pseudo-selectors, or descendant styles).
*
* Note that this method does not deal with nesting used for e.g.
* psuedo-selectors or media queries. That responsibility is left to the
* `generateCSS` function.
*
* @param {string} selector: the selector associated with the ruleset
* @param {Object} declarations: a map from camelCased CSS property name to CSS
* property value.
* @param {Object.<string, function>} stringHandlers: a map from camelCased CSS
* property name to a function which will map the given value to the value
* that is output.
* @param {bool} useImportant: A boolean saying whether to append "!important"
* to each of the CSS declarations.
* @returns {string} A string of raw CSS.
*
* Examples:
*
* generateCSSRuleset(".blah", { color: "red" })
* -> ".blah{color: red !important;}"
* generateCSSRuleset(".blah", { color: "red" }, {}, false)
* -> ".blah{color: red}"
* generateCSSRuleset(".blah", { color: "red" }, {color: c => c.toUpperCase})
* -> ".blah{color: RED}"
* generateCSSRuleset(".blah:hover", { color: "red" })
* -> ".blah:hover{color: red}"
*/
export const generateCSSRuleset = (selector, declarations, stringHandlers,
useImportant) => {
const handledDeclarations = runStringHandlers(
Expand Down

0 comments on commit 29cb180

Please sign in to comment.