1.1.1 • Published 5 months ago

@littlethings/css v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@littlethings/css

A small css-in-js utility.

Installation

# Using npm
npm install @littlethings/css

# Using yarn
yarn add @littlethings/css

Usage

Import

LittleCSS exposes a few named exports.

import { css, glob, keyframes, clsx } from "@littlethings/css";

css

The css utility generates a unique class name for a set of styles.

const myClass = css({
	"font-style": "italic",
	// camelCase is also supported
	backgroundColor: "pink",
});

This utility can also be used as a tagged template function.

const myClass = css`
	font-style: italic;
	background-color: pink;
`;

It is also possible to nest styles.

const myClass = css({
	"font-style": "italic",
	".inner-class": {
		"font-style": "initial",
	},
});

Nesting is supported in tagged templates as well.

const myClass = css`
	font-style: italic;

	.inner-class {
		font-style: initial;
	}
`;

glob

The glob utility applies CSS to the whole document.

glob({
	html: {
		background: "pink",
	},
});

This utility can also be used as a tagged template function.

glob`
	html {
		background: pink;
	}
`;

keyframes

The keyframes utility creates a keyframes definition to use in your other CSS.

const myAnimation = keyframes({
	from: {
		background: "orange",
	},
	to: {
		background: "rebeccapurple",
	},
});

// Use the animation in your CSS
const myClass = css({
	animation: `${myAnimation} 3s linear`,
});

This utility can also be used as a tagged template function.

const myAnimation = keyframes`
	from {
		background: orange;
	}
	to {
		background: rebeccapurple;
	}
`;

// Use the animation in your CSS
const myClass = css`
	animation: ${myAnimation} 3s linear;
`;

clsx

This utility is used to combine class names. This is necessary to ensure styles are overriden correctly.

const classA = css`
	color: yellow;
	background: white;
`;

const classB = css`
	color: red;
`;

const myClass = clsx(classA, classB);
// `myClass` will apply the following styles
// color: red;
// background: white;

The clsx utility also supports passing falsey values like undefined, null, and false for convenient toggling of classes during use. See the following code for an example.

// This example shows usage in a Preact component
const MyComponent = ({ isError, children }) => {
	const baseClass = css`
		color: black;
	`;

	const errorClass = css`
		color: white;
		background: red;
	`;

	return <div class={clsx(baseClass, isError && errorClass)}>{children}</div>;
};
1.1.1

5 months ago

1.1.0

5 months ago

1.0.7

1 year ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago