Skip to content

Instantly share code, notes, and snippets.

@alixcan
Forked from parafeu/scrollbar.js
Created April 16, 2022 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alixcan/251192a46db41a5237a2fe6f61eb7cb6 to your computer and use it in GitHub Desktop.
Save alixcan/251192a46db41a5237a2fe6f61eb7cb6 to your computer and use it in GitHub Desktop.
Scrollbar plugin tailwindcss
const plugin = require("tailwindcss/plugin");
module.exports = plugin(function ({ addUtilities, matchUtilities, theme }) {
const scrollbarTrackColorValue = (value) => ({
'--scrollbar-track': value,
'&::-webkit-scrollbar-track': {
"background-color": value
}
})
const scrollbarTrackRoundedValue = (value) => ({
'&::-webkit-scrollbar-track': {
"border-radius": value
}
});
const scrollbarThumbColorValue = (value) => ({
'--scrollbar-thumb': value,
'&::-webkit-scrollbar-thumb': {
"background-color": value
}
});
const scrollbarThumbRoundedValue = (value) => ({
'&::-webkit-scrollbar-thumb': {
"border-radius": value
}
});
addUtilities({
'.scrollbar': {
'--scrollbar-thumb': '#cdcdcd',
'--scrollbar-track': '#f0f0f0',
'--scrollbar-width': '17px',
'scrollbar-color': 'var(--scrollbar-thumb) var(--scrollbar-track)',
'&::-webkit-scrollbar': {
'width': 'var(--scrollbar-width)',
'height': 'var(--scrollbar-width)'
}
},
'.scrollbar-thin': {
'--scrollbar-width': '8px',
'scrollbar-width': 'thin'
}
});
Object.entries(theme('colors')).forEach(([colorName, color]) => {
switch (typeof color) {
case 'object':
matchUtilities(
{
[`scrollbar-track-${colorName}`]: (value) => (scrollbarTrackColorValue(value)),
[`scrollbar-thumb-${colorName}`]: (value) => (scrollbarThumbColorValue(value))
},
{
values: color
}
)
break;
case 'function':
addUtilities({
[`.scrollbar-track-${colorName}`]: scrollbarTrackColorValue(color({})),
[`.scrollbar-thumb-${colorName}`]: scrollbarThumbColorValue(color({}))
})
break;
case 'string':
addUtilities({
[`.scrollbar-track-${colorName}`]: scrollbarTrackColorValue(color),
[`.scrollbar-thumb-${colorName}`]: scrollbarThumbColorValue(color)
})
break;
}
});
matchUtilities(
{
'scrollbar-track-rounded': (value) => (scrollbarTrackRoundedValue(value)),
'scrollbar-thumb-rounded': (value) => (scrollbarThumbRoundedValue(value))
},
{
values: theme('borderRadius')
}
)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment