├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── cjs ├── index.js └── package.json ├── es.js ├── esm └── index.js ├── index.js ├── min.js ├── package.json ├── rollup ├── babel.config.js └── es.config.js └── test ├── index.html ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .nyc_output/ 2 | node_modules/ 3 | package-lock.json 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .nyc_output/ 2 | node_modules/ 3 | rollup/ 4 | test/ 5 | package-lock.json 6 | .travis.yml 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - stable 4 | git: 5 | depth: 1 6 | branches: 7 | only: 8 | - master 9 | after_success: 10 | - "npm run coveralls" 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2020, Andrea Giammarchi, @WebReflection 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 | OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 | PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # µstyler 2 | 3 | [![Build Status](https://travis-ci.com/WebReflection/ustyler.svg?branch=master)](https://travis-ci.com/WebReflection/ustyler) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/ustyler/badge.svg?branch=master)](https://coveralls.io/github/WebReflection/ustyler?branch=master) 4 | 5 | A minimalistic CSS style injector, usable either as function or as template literal tag. 6 | 7 | ```js 8 | import css from 'ustyler'; 9 | // const css = require('ustyler'); 10 | 11 | const style = css` 12 | body { 13 | font-family: sans-serif; 14 | } 15 | `; 16 | 17 | setTimeout(() => style.remove(), 1000); 18 | ``` 19 | -------------------------------------------------------------------------------- /cjs/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /** 3 | * Create, append, and return, a style node with the passed CSS content. 4 | * @param {string|string[]} template the CSS text or a template literal array. 5 | * @param {...any} values the template literal interpolations. 6 | * @return {HTMLStyleElement} the node appended as head last child. 7 | */ 8 | function ustyler(template) { 9 | const text = typeof template == 'string' ? [template] : [template[0]]; 10 | for (let i = 1, {length} = arguments; i < length; i++) 11 | text.push(arguments[i], template[i]); 12 | const style = document.createElement('style'); 13 | style.type = 'text/css'; 14 | style.appendChild(document.createTextNode(text.join(''))); 15 | return document.head.appendChild(style); 16 | } 17 | module.exports = ustyler; 18 | -------------------------------------------------------------------------------- /cjs/package.json: -------------------------------------------------------------------------------- 1 | {"type":"commonjs"} -------------------------------------------------------------------------------- /es.js: -------------------------------------------------------------------------------- 1 | var ustyle=function(e){"use strict";return e.default=function(e){const t="string"==typeof e?[e]:[e[0]];for(let n=1,{length:c}=arguments;n 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | global.document = { 2 | createElement: name => ({ 3 | name, 4 | appendChild(child) { 5 | return (this.child = child); 6 | } 7 | }), 8 | createTextNode(text) { 9 | return {name: '#text', text}; 10 | } 11 | }; 12 | 13 | document.head = document.createElement('head'); 14 | 15 | const css = require('../cjs'); 16 | 17 | const style = css('body{color:green}'); 18 | 19 | console.assert(document.head.child === style); 20 | console.assert(style.name === 'style', 'correct name'); 21 | console.assert(style.type === 'text/css', 'correct type'); 22 | console.assert(style.child.name === '#text', 'correct text node'); 23 | console.assert(style.child.text === 'body{color:green}', 'correct css text'); 24 | 25 | const tpl = css`body{color:${'red'}}`; 26 | 27 | console.assert(document.head.child === tpl); 28 | console.assert(tpl.name === 'style', 'correct name'); 29 | console.assert(tpl.type === 'text/css', 'correct type'); 30 | console.assert(tpl.child.name === '#text', 'correct text node'); 31 | console.assert(tpl.child.text === 'body{color:red}', 'correct css text'); 32 | -------------------------------------------------------------------------------- /test/package.json: -------------------------------------------------------------------------------- 1 | {"type":"commonjs"} --------------------------------------------------------------------------------