Skip to content
This repository was archived by the owner on Aug 3, 2020. It is now read-only.

Commit caf7433

Browse files
hacknugjoshmanders
authored andcommitted
Add support for Tailwind v1.x (#6)
* Add support for Tailwind v1.x * Add TravisCI config file * Update README for v1.0
1 parent 03e6834 commit caf7433

File tree

10 files changed

+4132
-4465
lines changed

10 files changed

+4132
-4465
lines changed

.babelrc

-13
This file was deleted.

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: node_js
2+
node_js:
3+
- "8"
4+
- "10"
5+
- "12"
6+
7+
cache:
8+
directories:
9+
- node_modules
10+
11+
before_install:
12+
- curl -L https://unpkg.com/@pnpm/self-installer | node
13+
14+
script:
15+
- pnpm install
16+
- pnpm run test

index.js

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const _ = require('lodash')
2+
const flatten = require('flat')
3+
4+
5+
const FLATTEN_CONFIG = { delimiter: '-', maxDepth: 2 }
6+
const handleName = (name, className) => {
7+
const split = name.split(`${className}-`)
8+
const prefixedName = `${split[0]}${prefixNegativeModifiers(className, split[1])}`
9+
10+
return prefixedName.split('-default').join('')
11+
}
12+
const prefixNegativeModifiers = function(base, modifier) {
13+
return _.startsWith(modifier, '-')
14+
? `-${base}-${modifier.slice(1)}`
15+
: `${base}-${modifier}`
16+
}
17+
18+
19+
module.exports = function () {
20+
return function ({
21+
addUtilities, addComponents, addBase, addVariant,
22+
e, prefix, theme, variants, config,
23+
}) {
24+
const buildConfig = (themeKey, ...fallbackKeys) => {
25+
return buildConfigFromTheme(themeKey, ...fallbackKeys) || buildConfigFromArray(themeKey)
26+
}
27+
const buildConfigFromTheme = (themeKey, ...fallbackKeys) => {
28+
const buildObject = ([ modifier, value ]) => [ modifier, { [themeKey]: value } ]
29+
const getThemeSettings = (themeKey, fallbackKeys) => {
30+
const [newThemeKey, ...newFallbackKeys] = fallbackKeys || []
31+
32+
return theme(themeKey, false) || (fallbackKeys.length && getThemeSettings(newThemeKey, [...newFallbackKeys]))
33+
}
34+
35+
const themeSettings = getThemeSettings(themeKey, fallbackKeys)
36+
const themeObject = _.isArray(themeSettings) ? _.zipObject(themeSettings, themeSettings) : themeSettings
37+
const themeEntries = themeSettings && Object
38+
.entries(flatten(themeObject, FLATTEN_CONFIG))
39+
.map(entry => buildObject(entry))
40+
41+
return themeSettings ? _.fromPairs(themeEntries) : false
42+
}
43+
const buildConfigFromArray = (property) => {
44+
const defaultSettings = defaultValues[property]
45+
const defaultEntries = defaultSettings && defaultSettings
46+
.map(value => ([value, { [property]: value }]))
47+
48+
return defaultSettings ? _.fromPairs(defaultEntries) : false
49+
}
50+
51+
const buildPluginUtilityObject = ({ color, size, border, speed }) => {
52+
return {
53+
'position': 'relative',
54+
'color': 'transparent !important',
55+
'pointer-events': 'none',
56+
57+
'&::after': {
58+
'content': `''`,
59+
60+
'position': 'absolute !important',
61+
'top': `calc(50% - (${size} / 2))`,
62+
'left': `calc(50% - (${size} / 2))`,
63+
64+
'display': 'block',
65+
'width': size,
66+
'height': size,
67+
68+
'border': `${border} solid ${color}`,
69+
'border-radius': '9999px',
70+
'border-right-color': 'transparent',
71+
'border-top-color': 'transparent',
72+
73+
'animation': `spinAround ${speed} infinite linear`,
74+
},
75+
}
76+
}
77+
78+
const buildDefaultValuesObject = (defaultConfig, themeKey, ...fallbackKeys) => {
79+
const defaultEntries = Object.entries(theme(themeKey, { default: defaultConfig }))
80+
.map(([ modifier, config ]) => [ modifier, buildPluginUtilityObject({ ...defaultConfig, ...config }) ])
81+
82+
return _.fromPairs(defaultEntries)
83+
}
84+
85+
const defaultValues = {}
86+
const defaultConfig = {
87+
color: 'currentColor',
88+
size: '1em',
89+
border: '2px',
90+
speed: '500ms',
91+
}
92+
93+
const pluginUtilities = {
94+
spinner: buildDefaultValuesObject(defaultConfig, 'spinner'),
95+
}
96+
97+
Object.entries(pluginUtilities)
98+
.filter(([ modifier, values ]) => !_.isEmpty(values))
99+
.forEach(([ modifier, values ]) => {
100+
const className = _.kebabCase(modifier)
101+
const variantName = Object.keys(Object.entries(values)[0][1])[0]
102+
const utilities = flatten({ [`.${e(`${className}`)}`]: values }, FLATTEN_CONFIG)
103+
104+
addUtilities(
105+
_.mapKeys(utilities, (value, key) => handleName(key, className)),
106+
variants(variantName, ['responsive'])
107+
)
108+
})
109+
110+
addUtilities({
111+
'@keyframes spinAround': {
112+
'from': { 'transform': 'rotate(0deg)' },
113+
'to': { 'transform': 'rotate(360deg)' },
114+
},
115+
})
116+
}
117+
}

package.json

+18-34
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,29 @@
22
"name": "tailwindcss-spinner",
33
"version": "0.2.0",
44
"description": "Spinner utility for Tailwind CSS",
5-
"main": "lib/index.js",
6-
"repository": "git@github.com:aniftyco/tailwindcss-spinner.git",
7-
"author": "Josh Manders <josh@joshmanders.com>",
8-
"license": "MIT",
9-
"files": [
10-
"lib/"
11-
],
125
"keywords": [
6+
"plugin",
7+
"tailwind",
8+
"tailwind css",
139
"tailwindcss",
14-
"tailwind"
10+
"tailwindcss-plugin"
1511
],
12+
"repository": "git@github.com:aniftyco/tailwindcss-spinner.git",
13+
"license": "MIT",
14+
"author": "Josh Manders <josh@joshmanders.com>",
15+
"main": "index.js",
1616
"scripts": {
17-
"prebuild": "rimraf lib/",
18-
"build": "babel src/ --out-dir lib/",
19-
"test": "jest",
20-
"prepublish": "yarn build"
17+
"dev": "jest --watchAll",
18+
"test": "jest"
2119
},
22-
"devDependencies": {
23-
"babel-cli": "^6.26.0",
24-
"babel-plugin-add-module-exports": "^0.2.1",
25-
"babel-plugin-transform-object-rest-spread": "^6.26.0",
26-
"babel-preset-env": "^1.6.1",
27-
"eslint": "^4.19.1",
28-
"eslint-config-nifty": "^0.4.1",
29-
"eslint-plugin-jest": "^21.15.0",
30-
"jest": "^22.4.3",
31-
"rimraf": "^2.6.2"
20+
"dependencies": {
21+
"flat": "^4.1.0",
22+
"lodash": "^4.17.11"
3223
},
33-
"jest": {
34-
"testEnvironment": "node",
35-
"collectCoverageFrom": [
36-
"!node_modules/**",
37-
"!tests/**",
38-
"src/**"
39-
],
40-
"coverageThreshold": {
41-
"global": {
42-
"statements": 0
43-
}
44-
}
24+
"devDependencies": {
25+
"jest": "^24.8.0",
26+
"jest-matcher-css": "^1.0.3",
27+
"postcss": "^7.0.16",
28+
"tailwindcss": "^1.0.1"
4529
}
4630
}

0 commit comments

Comments
 (0)