Skip to content

Commit e66f19e

Browse files
committed
added index.js back temporary
1 parent 860a09a commit e66f19e

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

src/index.js

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
// import { withOptions } from 'tailwindcss/plugin'
2+
const plugin = require('tailwindcss/plugin')
3+
4+
const themeKey = 'scrollbar' // theme.scrollbar
5+
const darkClass = 'dark'
6+
7+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8+
const omit = (key, { [key]: _, ...obj }) => obj
9+
10+
/**
11+
* Handle plugin.withOptions and theme.scrollbar.DEFAULT
12+
*/
13+
const getDefaultStyle = (options, pluginAPI) => {
14+
const { theme, config } = pluginAPI
15+
16+
const getSize = () => {
17+
return options?.size ?? theme(`${themeKey}.DEFAULT.size`, '5px')
18+
}
19+
const getStyleTrack = () => {
20+
const background = '#f1f1f1' // default
21+
const fromConfig = theme(`${themeKey}.DEFAULT.track`, {}) // with tailwind.config.js
22+
const fromOptions = options?.track ?? {} // with plugin options
23+
24+
const finalConfig = { background, ...fromConfig, ...fromOptions }
25+
26+
if (! finalConfig.darkBackground) {
27+
finalConfig.darkBackground = finalConfig.background
28+
}
29+
30+
return finalConfig
31+
}
32+
const getStyleThumb = () => {
33+
const background = '#c1c1c1'
34+
const fromConfig = theme(`${themeKey}.DEFAULT.thumb`, {}) // with tailwind.config.js
35+
const fromOptions = options?.thumb ?? {} // with plugin options
36+
37+
const finalConfig = { background, ...fromConfig, ...fromOptions }
38+
39+
return finalConfig
40+
}
41+
const getStyleThumbHover = () => {
42+
const background = '#a8a8a8'
43+
const fromConfig = theme(`${themeKey}.DEFAULT.hover`, {}) // with tailwind.config.js
44+
const fromOptions = options?.hover ?? {} // with plugin options
45+
46+
const finalConfig = { background, ...fromConfig, ...fromOptions }
47+
48+
return finalConfig
49+
}
50+
51+
const size = getSize()
52+
const track = getStyleTrack()
53+
const thumb = getStyleThumb()
54+
const hover = getStyleThumbHover()
55+
56+
const styles = [
57+
{
58+
'::-webkit-scrollbar': {
59+
width: size,
60+
height: size,
61+
},
62+
'::-webkit-scrollbar-track': omit('darkBackground', track),
63+
'::-webkit-scrollbar-thumb': omit('darkBackground', thumb),
64+
'::-webkit-scrollbar-thumb:hover': omit('darkBackground', hover),
65+
},
66+
]
67+
68+
const dark = {
69+
'::-webkit-scrollbar-track': {
70+
background: track.darkBackground ?? track.background,
71+
},
72+
'::-webkit-scrollbar-thumb': {
73+
background: thumb.darkBackground ?? thumb.background,
74+
},
75+
'::-webkit-scrollbar-thumb:hover': {
76+
background: hover.darkBackground ?? hover.background,
77+
},
78+
}
79+
const light = {
80+
'::-webkit-scrollbar-track': {
81+
background: track.background,
82+
},
83+
'::-webkit-scrollbar-thumb': {
84+
background: thumb.background,
85+
},
86+
'::-webkit-scrollbar-thumb:hover': {
87+
background: hover.background,
88+
},
89+
}
90+
91+
if (config('darkMode') === 'media') {
92+
styles.push({
93+
'@media (prefers-color-scheme: dark)': dark,
94+
'@media (prefers-color-scheme: light)': light,
95+
})
96+
} else {
97+
styles.push({
98+
[`.${darkClass}`]: dark,
99+
})
100+
}
101+
102+
return styles
103+
}
104+
105+
/**
106+
* Handle theme.scrollbar.<any key>
107+
*/
108+
const getCustomStyles = (pluginAPI) => {
109+
const { theme, config } = pluginAPI
110+
111+
const styles = Object.entries(theme(themeKey, {}))
112+
.filter(([key]) => key !== 'DEFAULT')
113+
.map(([key, val]) => {
114+
const className = `.${themeKey}-${key}`
115+
116+
const { size, track, thumb, hover } = val
117+
118+
if (!size) {
119+
throw new Error(`[@gradin/tailwindcss-scrollbar] theme.${themeKey}.${key} should have property [size].`)
120+
}
121+
if (!track) {
122+
throw new Error(`[@gradin/tailwindcss-scrollbar] theme.${themeKey}.${key} should have property [size].`)
123+
}
124+
if (!thumb) {
125+
throw new Error(`[@gradin/tailwindcss-scrollbar] theme.${themeKey}.${key} should have property [size].`)
126+
}
127+
if (!hover) {
128+
throw new Error(`[@gradin/tailwindcss-scrollbar] theme.${themeKey}.${key} should have property [size].`)
129+
}
130+
131+
return {
132+
[`${className}::-webkit-scrollbar`]: {
133+
width: size,
134+
height: size,
135+
},
136+
[`${className}::-webkit-scrollbar-track`]: omit('darkBackground', track),
137+
[`${className}::-webkit-scrollbar-thumb`]: omit('darkBackground', thumb),
138+
[`${className}::-webkit-scrollbar-thumb:hover`]: omit('darkBackground', hover),
139+
}
140+
})
141+
142+
const dark = Object.entries(theme(themeKey, {}))
143+
.filter(([key]) => key !== 'DEFAULT')
144+
.map(([key, val]) => {
145+
const className = `.${themeKey}-${key}`
146+
const value = val
147+
const track = value.track ?? {}
148+
const thumb = value.thumb ?? {}
149+
const hover = value.hover ?? {}
150+
151+
return {
152+
[`${className}::-webkit-scrollbar-track`]: {
153+
background: track.darkBackground ?? track.background,
154+
},
155+
[`${className}::-webkit-scrollbar-thumb`]: {
156+
background: thumb.darkBackground ?? thumb.background,
157+
},
158+
[`${className}::-webkit-scrollbar-thumb:hover`]: {
159+
background: hover.darkBackground ?? hover.background,
160+
},
161+
}
162+
})
163+
const light = Object.entries(theme(themeKey, {}))
164+
.filter(([key]) => key !== 'DEFAULT')
165+
.map(([key, val]) => {
166+
const className = `.${themeKey}-${key}`
167+
const value = val
168+
const track = value.track ?? {}
169+
const thumb = value.thumb ?? {}
170+
const hover = value.hover ?? {}
171+
172+
return {
173+
[`${className}::-webkit-scrollbar-track`]: {
174+
background: track.background,
175+
},
176+
[`${className}::-webkit-scrollbar-thumb`]: {
177+
background: thumb.background,
178+
},
179+
[`${className}::-webkit-scrollbar-thumb:hover`]: {
180+
background: hover.background,
181+
},
182+
}
183+
})
184+
185+
if (config('darkMode') === 'media') {
186+
styles.push({
187+
'@media (prefers-color-scheme: dark)': dark,
188+
'@media (prefers-color-scheme: light)': light,
189+
})
190+
} else {
191+
dark.forEach(s => {
192+
styles.push({
193+
[`.${darkClass}`]: s,
194+
})
195+
})
196+
}
197+
198+
return styles
199+
}
200+
201+
const scrollbarNoneStyle = [
202+
{
203+
[`.${themeKey}-none`]: {
204+
'-ms-overflow-style': 'none', /* IE and Edge */
205+
'scrollbar-width': 'none', /* Firefox */
206+
},
207+
},
208+
{
209+
[`.${themeKey}-none::-webkit-scrollbar`]: {
210+
'display': 'none', /* Chrome, Safari, Opera */
211+
},
212+
},
213+
]
214+
215+
module.exports = plugin.withOptions(function (options) {
216+
return function (pluginAPI) {
217+
const { addBase, addUtilities } = pluginAPI
218+
219+
addBase(getDefaultStyle(options, pluginAPI))
220+
221+
addUtilities(getCustomStyles(pluginAPI))
222+
223+
addUtilities(scrollbarNoneStyle)
224+
}
225+
})

0 commit comments

Comments
 (0)