|
| 1 | +/** |
| 2 | + * Material Design 3 Checkbox Component - Tailwind Plugin |
| 3 | + */ |
| 4 | + |
| 5 | +import plugin from 'tailwindcss/plugin'; |
| 6 | + |
| 7 | +export const checkboxPlugin: ReturnType<typeof plugin> = plugin(function ({ addComponents }) { |
| 8 | + const checkboxes = { |
| 9 | + '.md-checkbox': { |
| 10 | + '@apply relative inline-flex items-center justify-center w-10 h-10 cursor-pointer rounded-md-full hover:bg-md-on-surface/10 transition-colors': {}, |
| 11 | + |
| 12 | + 'input[type="checkbox"]': { |
| 13 | + '@apply appearance-none w-4.5 h-4.5 border-2 border-md-on-surface-variant rounded-md-xs bg-transparent outline-none transition-all duration-200': {}, |
| 14 | + |
| 15 | + '&:checked': { |
| 16 | + '@apply bg-md-primary border-md-primary': {}, |
| 17 | + // Checkmark icon styling could be done with a background image or SVG, |
| 18 | + // but for a pure CSS/Tailwind solution, we can use a pseudo-element or expect an SVG sibling. |
| 19 | + // Here we'll use a mask or background-image approach if we want pure CSS, |
| 20 | + // or rely on the HTML structure having an SVG. |
| 21 | + // For simplicity in this plugin, we'll style the input itself. |
| 22 | + |
| 23 | + '&:before': { |
| 24 | + content: '""', |
| 25 | + '@apply absolute inset-0 flex items-center justify-center text-md-on-primary': {}, |
| 26 | + backgroundImage: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23ffffff'%3E%3Cpath d='M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z'/%3E%3C/svg%3E")`, |
| 27 | + backgroundSize: '100%', |
| 28 | + backgroundPosition: 'center', |
| 29 | + backgroundRepeat: 'no-repeat', |
| 30 | + } |
| 31 | + }, |
| 32 | + |
| 33 | + '&:disabled': { |
| 34 | + '@apply border-md-on-surface/38 cursor-not-allowed': {}, |
| 35 | + '&:checked': { |
| 36 | + '@apply bg-md-on-surface/38 border-transparent': {}, |
| 37 | + } |
| 38 | + }, |
| 39 | + |
| 40 | + '&:focus-visible': { |
| 41 | + '@apply ring-2 ring-md-primary ring-offset-2': {}, |
| 42 | + }, |
| 43 | + |
| 44 | + // Error state |
| 45 | + '&.error': { |
| 46 | + '@apply border-md-error': {}, |
| 47 | + '&:checked': { |
| 48 | + '@apply bg-md-error border-md-error': {}, |
| 49 | + }, |
| 50 | + '&:focus-visible': { |
| 51 | + '@apply ring-md-error': {}, |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + addComponents(checkboxes); |
| 59 | +}); |
| 60 | + |
| 61 | +export default checkboxPlugin; |
0 commit comments