DefinitelyTyped/types/stylis/stylis-tests.ts
Gen Hames 371abc4ec3
Add types for Stylis 4.0 (#46171)
* Types for Stylis 4.0

* Add default export for Stylis

Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com>

* Improved T generics

https://github.com/thysultan/stylis.js/pull/231#discussion_r456833622

Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com>
2020-07-21 11:36:02 -07:00

65 lines
1.5 KiB
TypeScript

import { compile, serialize, stringify, middleware, DECLARATION } from "stylis";
const styles = `
.class {
margin-top: 5px;
}
`;
// $ExpectType Element[]
const AST = compile(styles);
// $ExpectType string
const A = serialize(compile(styles), stringify);
const assert = (bool: boolean): void => { if (bool) return; throw new Error(); };
// Traversal example
const B = serialize(
compile('h1{all:unset}'),
middleware([
(element, index, children) => {
assert(children === element.root.children && children[index] === element.children);
},
stringify
])
);
assert(B === 'h1{all:unset;}');
// Prefixing example
const C = serialize(
compile('h1{all:unset}'),
middleware([
(element, index, children, callback) => {
if (element.type === DECLARATION && element.props === 'all' && element.children === 'unset')
element.return = 'color:red;' + element.value;
},
stringify
])
);
assert(C === 'h1{color:red;all:unset;}');
const D = serialize(
compile('h1{all:unset}'),
middleware([
(element, index, children, callback) => {
if (element.type === 'rule' && element.props.indexOf('h1') > -1)
return serialize([{...element, props: ['h2', 'h3']}], callback);
},
stringify
])
);
assert(D === 'h2,h3{all:unset;}h1{all:unset;}');
// Reading example
const E = serialize(
compile('h1{all:unset}'),
middleware([
stringify,
(element, index, children) => {
assert(element.return === 'h1{all:unset;}');
}
])
);
assert(E === 'h1{all:unset;color:red;}');