Skip to content

Instantly share code, notes, and snippets.

@parafeu
Last active September 3, 2022 19:29
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save parafeu/3cf1c52d374a52091a685ce030563411 to your computer and use it in GitHub Desktop.
Save parafeu/3cf1c52d374a52091a685ce030563411 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')
}
)
});
@webmastervinay
Copy link

How to further use this plugin? Any documentation?

@parafeu
Copy link
Author

parafeu commented Jan 4, 2022

Hi ! You just have to require the js file in your plugins of your tailwind.config.js.
plugins: [ require('./src/plugins/tailwindcss/scrollbar.js') ]

It should work like adoxography/tailwind-scrollbar.

@augustmuir
Copy link

This works great thankyou.
scrollbar scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-zinc-300

@oxwazz
Copy link

oxwazz commented Jan 31, 2022

this work on tailwind v3, thank you very much

@NecipAkgz
Copy link

All of them working except scrollbar-thin

@augustmuir
Copy link

@NecipAkgz scrollbar-thin works fine for me, try the snippet I commented above.

@alixcan
Copy link

alixcan commented Apr 14, 2022

Thanks alot,
I've just tried this on my laravel project with webpack mix
scrollbar-thin works on y axis but not working on x axis

@parafeu
Copy link
Author

parafeu commented Apr 14, 2022

Can you try the revision i just made ? I forgot to apply height on ::-webkit-scrollbar pseudoelement

@alixcan
Copy link

alixcan commented Apr 16, 2022

Thanks work purfectly <3

@ahoward1024
Copy link

@parafeu There is a missing style here. The scrollbar-corner is styled automatically with a white background:
scrollbar scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-lime-600
Screen Shot 2022-04-27 at 12 36 14

I fixed this by adding the color styles for the corner element:
scrollbar scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-lime-600 scrollbar-corner-transparent

Screen Shot 2022-04-27 at 12 36 40

I've updated the code in my own fork to reflect both setting the color and rounding values to the corner element, but I will post it for posterity as well here:

Code
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
        }
    });

    const scrollbarCornerColorValue = (value) => ({
        '&::-webkit-scrollbar-corner': {
            "background-color": value
        }
    });

    const scrollbarCornerRoundedValue = (value) => ({
        '&::-webkit-scrollbar-corner': {
            "border-radius": value
        }
    });

    addUtilities({
        '.scrollbar': {
            '--scrollbar-thumb': '#cdcdcd',
            '--scrollbar-track': '#f0f0f0',
            '--scrollbar-corner': '#f0f0f0',
            '--scrollbar-width': '17px',
            'scrollbar-color': 'var(--scrollbar-thumb) var(--scrollbar-track) var(--scrollbar-corner)',
            '&::-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)),
                        [`scrollbar-corner-${colorName}`]: (value) => (scrollbarCornerColorValue(value))
                    },
                    {
                        values: color
                    }
                )
                break;
            case 'function':
                addUtilities({
                    [`.scrollbar-track-${colorName}`]: scrollbarTrackColorValue(color({})),
                    [`.scrollbar-thumb-${colorName}`]: scrollbarThumbColorValue(color({})),
                    [`scrollbar-corner-${colorName}`]: (scrollbarCornerColorValue({}))
                })
                break;
            case 'string':
                addUtilities({
                    [`.scrollbar-track-${colorName}`]: scrollbarTrackColorValue(color),
                    [`.scrollbar-thumb-${colorName}`]: scrollbarThumbColorValue(color),
                    [`.scrollbar-corner-${colorName}`]: scrollbarCornerColorValue(color)
                })
                break;
        }
    });

    matchUtilities(
        {
            'scrollbar-track-rounded': (value) => (scrollbarTrackRoundedValue(value)),
            'scrollbar-thumb-rounded': (value) => (scrollbarThumbRoundedValue(value)),
            'scrollbar-corner-rounded': (value) => (scrollbarCornerRoundedValue(value))
        },
        {
            values: theme('borderRadius')
        }
    )
});

@SimonWpt
Copy link

SimonWpt commented Jul 4, 2022

This does not work, if you use css variables.
https://tailwindcss.com/docs/customizing-colors#using-css-variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment