tailwind.smooth-shadow.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Smooth shadows for Tailwind CSS 3.
  3. *
  4. * A port of the `shadow-plugin` package by Florian Kiem (MIT, https://shadow.floriankiem.com),
  5. * whose published build targets Tailwind 4 only (`@theme` / `@utility`). Class names and shadow
  6. * values are identical, so switching to the npm package once UNA moves to Tailwind 4 needs no
  7. * template changes.
  8. *
  9. * - `smooth-shadow-{xs|sm|md|lg|xl|2xl}` — stacked, eased shadow layers (`smooth-shadow` = md).
  10. * - `smooth-shadow-ring-{size}` — the same stack with a 1px hairline ring baked in as the last
  11. * layer, for elevated surfaces (blocks, cards, popups, menus). Never pair it with `border`/`ring`.
  12. * - `shadow-{color}` tints the shadow, `smooth-ring-{color}` tints the ring; both compose.
  13. * - The ring is black 5% in light mode and white 18% under `.dark` / `[data-theme="dark"]`.
  14. */
  15. const plugin = require('tailwindcss/plugin');
  16. const flattenColorPalette = require('tailwindcss/lib/util/flattenColorPalette').default;
  17. // [offset-y, blur, spread, alpha %]
  18. const LAYERS = {
  19. xs: [[0, 4, 0, 4]],
  20. sm: [[18, 47, 0, 3], [7.5, 19, 0, 2], [4, 10.5, 0, 2], [2.3, 5.8, 0, 1], [1.2, 3.1, 0, 1], [0.5, 1.3, 0, 1]],
  21. md: [[17.54, 23.39, 0, 4], [9.4, 12.5, 0, 3], [5.25, 7, 0, 2], [2.79, 3.72, -2, 1], [1.16, 1.5, 0, 1]],
  22. lg: [[25, 50, 0, 5], [12, 24, 0, 4], [6, 12, 0, 3], [3, 6, 0, 2], [1.5, 3, 0, 2]],
  23. xl: [[40, 80, 0, 6], [20, 40, 0, 5], [10, 20, 0, 4], [5, 10, 0, 3], [2, 4, 0, 2]],
  24. '2xl': [[60, 120, 0, 7], [30, 60, 0, 6], [15, 30, 0, 5], [7.5, 15, 0, 4], [3, 6, 0, 3]],
  25. };
  26. const RING = '0 0 0 var(--smooth-ring-width, 1px) var(--smooth-ring-color)';
  27. function stack(size, withRing) {
  28. const layers = LAYERS[size].map(([y, blur, spread, alpha]) =>
  29. `0 ${y}px ${blur}px ${spread}px color-mix(in srgb, var(--smooth-shadow-color) ${alpha}%, transparent)`
  30. );
  31. if (withRing)
  32. layers.push(RING);
  33. return layers.join(', ');
  34. }
  35. module.exports = plugin(function ({ addBase, addUtilities, matchUtilities, theme }) {
  36. addBase({
  37. ':root': {
  38. '--smooth-ring-color': 'rgba(0, 0, 0, 0.05)',
  39. '--smooth-ring-width': '1px',
  40. },
  41. '.light, [data-theme="light"]': {
  42. '--smooth-ring-color': 'rgba(0, 0, 0, 0.05)',
  43. },
  44. '.dark, [data-theme="dark"]': {
  45. '--smooth-ring-color': 'rgba(255, 255, 255, 0.18)',
  46. },
  47. });
  48. const utilities = {
  49. '.smooth-shadow-none': { boxShadow: 'none' },
  50. };
  51. /**
  52. * Like Tailwind's own `shadow-*`, the stack is written to `--tw-shadow` and box-shadow is composed from
  53. * the ring variables, so `ring-*` utilities on the same element add to the shadow instead of replacing it.
  54. */
  55. const BOX_SHADOW = 'var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)';
  56. for (const size of Object.keys(LAYERS)) {
  57. for (const [name, withRing] of [[`smooth-shadow-${size}`, false], [`smooth-shadow-ring-${size}`, true]]) {
  58. const value = stack(size, withRing);
  59. utilities[`.${name}`] = {
  60. '--smooth-shadow-color': 'var(--tw-shadow-color, black)',
  61. '--tw-shadow': value,
  62. '--tw-shadow-colored': value,
  63. boxShadow: BOX_SHADOW,
  64. };
  65. }
  66. }
  67. utilities['.smooth-shadow'] = utilities['.smooth-shadow-md'];
  68. utilities['.smooth-shadow-ring'] = utilities['.smooth-shadow-ring-md'];
  69. addUtilities(utilities);
  70. matchUtilities(
  71. { 'smooth-ring': (value) => ({ '--smooth-ring-color': value }) },
  72. { values: flattenColorPalette(theme('colors')), type: 'color' }
  73. );
  74. });