Skip to content

Instantly share code, notes, and snippets.

@parafeu
Last active July 26, 2024 05:18
Show Gist options
  • 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')
}
)
});
@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

@Bishwas-py
Copy link

saved my life thanks

@Bishwas-py
Copy link

plugins: [require('@tailwindcss/typography'),
		plugin(function({ addUtilities, matchUtilities }) {
			matchUtilities(
				{
					'scroll-size': (value) => ({
						'&::-webkit-scrollbar': {
							width: `${value}px`,
							height: `${value}px`
						}
					})
				},
				{
					values: {
						'light': '1',
						2: '2',
						3: '3',
						4: '4',
						5: '5',
						6: '6',
						7: '7',
						8: '8',
						9: '9',
						'fat': '10'
					}
				}
			);
			addUtilities({
				'.scroll-invisible::-webkit-scrollbar': {
					'background-color': 'transparent'
				},
				'.scroll-visible::-webkit-scrollbar': {
					'background-color': 'initial'
				},
				'::-webkit-scrollbar': {
					width: '10px'
				},
				'.dark ::-webkit-scrollbar-track': {
					background: '#2f2f2f'
				},
				'.dark ::-webkit-scrollbar-thumb': {
					background: '#595757'
				},
				'.dark ::-webkit-scrollbar-thumb:hover': {
					background: '#706e6e'
				},
				'::-webkit-scrollbar-track': {
					background: '#c5c5c5'
				},
				'::-webkit-scrollbar-thumb': {
					background: '#8a8a8a'
				},
				'::-webkit-scrollbar-thumb:hover': {
					background: '#8a8a8a'
				}
			});
		})],

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