Skip to content

Commit 43db226

Browse files
authoredDec 2, 2020
feat: add tailwind colors page (#215)
* feat: add tailwind colors page * chore: don't check coverage for dev
1 parent 52cbf0c commit 43db226

File tree

3 files changed

+200
-2
lines changed

3 files changed

+200
-2
lines changed
 

‎lib/module.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = async function (moduleOptions) {
1212
configPath: 'tailwind.config.js',
1313
cssPath: join(nuxt.options.dir.assets, 'css', 'tailwind.css'),
1414
exposeConfig: false,
15-
config: defaultTailwindConfig(nuxt.options)
15+
config: defaultTailwindConfig(nuxt.options),
16+
ui: nuxt.options.dev
1617
})
1718

1819
const configPath = nuxt.resolver.resolveAlias(options.configPath)
@@ -66,11 +67,33 @@ module.exports = async function (moduleOptions) {
6667
postcss.plugins.tailwindcss = tailwindConfig
6768
}
6869

70+
/*
71+
** Add /_tailwind UI
72+
*/
73+
/* istanbul ignore if */
74+
if (nuxt.options.dev) {
75+
// add layout
76+
const layout = resolve(__dirname, 'ui', 'layouts', 'tw.vue')
77+
this.addLayout(layout, 'tw')
78+
// add page
79+
const page = resolve(__dirname, 'ui', 'pages', 'index.vue')
80+
81+
// register page
82+
this.extendRoutes((routes) => {
83+
routes.unshift({
84+
name: '_tailwind',
85+
path: '/_tailwind',
86+
component: page
87+
})
88+
})
89+
const url = nuxt.server.listeners ? nuxt.server.listeners[0].url : '/'
90+
logger.info(`Tailwind Colors available on ${url}_tailwind`)
91+
}
6992
/*
7093
** Expose resolved tailwind config as an alias
7194
** https://tailwindcss.com/docs/configuration/#referencing-in-javascript
7295
*/
73-
if (options.exposeConfig) {
96+
if (nuxt.options.dev || options.exposeConfig) {
7497
// Resolve config
7598
const resolveConfig = require('tailwindcss/resolveConfig')
7699
const resolvedConfig = resolveConfig(tailwindConfig)

‎lib/ui/layouts/tw.vue

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<Nuxt />
3+
</template>

‎lib/ui/pages/index.vue

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<template>
2+
<div class="container relative mx-auto px-2 py-6 md:px-12 max-w-4xl md:py-12">
3+
<h1>Tailwind Colors</h1>
4+
<div v-for="color of Object.keys(colors)" :key="color" class="mb-6">
5+
<h2>{{ color }}</h2>
6+
<span class="flex space-x-2">
7+
<span
8+
v-for="value of Object.keys(colors[color])"
9+
:key="value"
10+
class="color-box flex-grow py-6 md:py-8 rounded cursor-pointer"
11+
:class="value === 'DEFAULT' ? `bg-${color}` : `bg-${color}-${value}`"
12+
@click="select(color, value)"
13+
/>
14+
</span>
15+
</div>
16+
<div
17+
class="copy-box border-2 rounded-md bg_white"
18+
:class="boxClasses"
19+
>
20+
<span
21+
v-if="!selected"
22+
class="select-color"
23+
>Select a color</span>
24+
<div v-else>
25+
<pre
26+
:class="selected.bgClass"
27+
@click="copy(selected.bgClass)"
28+
>{{ copied === selected.bgClass ? 'Copied!' : selected.bgClass }}</pre>
29+
<pre
30+
:class="selected.textClass"
31+
@click="copy(selected.textClass)"
32+
>{{
33+
copied === selected.textClass ? 'Copied!' : selected.textClass
34+
}}</pre>
35+
</div>
36+
</div>
37+
</div>
38+
</template>
39+
40+
<script>
41+
import { theme } from '~tailwind.config'
42+
43+
export default {
44+
layout: 'tw',
45+
data () {
46+
// Remove unecessary colors
47+
delete theme.colors.white
48+
delete theme.colors.black
49+
delete theme.colors.current
50+
delete theme.colors.transparent
51+
52+
return {
53+
colors: theme.colors,
54+
selected: null,
55+
copied: ''
56+
}
57+
},
58+
computed: {
59+
boxClasses () {
60+
if (!this.selected) {
61+
return 'bg-white border-2 border-stone text-stone'
62+
}
63+
const textColor =
64+
this.selected.class.includes('dark') ||
65+
['500', '600', '700', '800', '900'].includes(this.selected.value)
66+
? 'text_white'
67+
: 'text_black bg_black'
68+
return `border-${this.selected.class} ${textColor}`
69+
}
70+
},
71+
methods: {
72+
select (color, value) {
73+
if (
74+
this.selected &&
75+
this.selected.color === color &&
76+
this.selected.value === value
77+
) {
78+
this.selected = null
79+
return
80+
}
81+
const twClass = value === 'DEFAULT' ? `${color}` : `${color}-${value}`
82+
this.selected = {
83+
color,
84+
value,
85+
class: twClass,
86+
bgClass: `bg-${twClass}`,
87+
textClass: `text-${twClass}`
88+
}
89+
},
90+
async copy (text) {
91+
if (!navigator.clipboard) { return }
92+
this._timeout && clearTimeout(this._timeout)
93+
await navigator.clipboard.writeText(text)
94+
this.copied = text
95+
this._timeout = setTimeout(() => {
96+
this.copied = ''
97+
}, 500)
98+
}
99+
}
100+
}
101+
</script>
102+
103+
<style scoped>
104+
/* compatibility with Tailwind V1 or overwriting some styles */
105+
h1 {
106+
font-size: 2.25rem;
107+
line-height: 2.5rem;
108+
font-weight: 600;
109+
margin-bottom: 2rem;
110+
}
111+
h2 {
112+
font-size: 1.5rem;
113+
line-height: 2rem;
114+
margin-bottom: 0.5rem;
115+
text-transform: capitalize;
116+
font-weight: 500;
117+
}
118+
.color-box {
119+
transition-property: transform;
120+
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
121+
transition-duration: 150ms;
122+
}
123+
.color-box:hover {
124+
transform: scale(1.1);
125+
}
126+
.copy-box {
127+
position: fixed;
128+
top: 1rem;
129+
right: 1rem;
130+
min-width: 14rem;
131+
height: 6rem;
132+
text-align: center;
133+
}
134+
.copy-box .select-color {
135+
line-height: 6rem;
136+
}
137+
.copy-box pre {
138+
cursor: pointer;
139+
height: 3rem;
140+
line-height: 3rem;
141+
padding: 0 1rem;
142+
}
143+
@media (min-width: 1420px) {
144+
.copy-box {
145+
top: 50%;
146+
margin-top: -4rem;
147+
right: 4rem;
148+
bottom: 3rem;
149+
height: 8rem;
150+
}
151+
.copy-box .select-color {
152+
line-height: 8rem;
153+
}
154+
.copy-box pre {
155+
height: 4rem;
156+
line-height: 4rem;
157+
}
158+
}
159+
160+
.text_white {
161+
color: white;
162+
}
163+
.text_black {
164+
color: black;
165+
}
166+
.bg_white {
167+
background: white;
168+
}
169+
.bg_black {
170+
background: #111;
171+
}
172+
</style>

0 commit comments

Comments
 (0)
Please sign in to comment.