tailwind.smooth-shadow.js 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * - `smooth-shadow-border` — a shadow in place of a border for surfaces that only need an edge (the
  15. * header bar, page blocks): a 1px ring, a 1px lift and a 2px ambient layer in light mode, and a single
  16. * white ring under `.dark`, where depth shadows don't show. Never pair it with `border`/`ring`.
  17. */
  18. const plugin = require('tailwindcss/plugin');
  19. const flattenColorPalette = require('tailwindcss/lib/util/flattenColorPalette').default;
  20. // [offset-y, blur, spread, alpha %]
  21. const LAYERS = {
  22. xs: [[0, 4, 0, 4]],
  23. 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]],
  24. 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]],
  25. lg: [[25, 50, 0, 5], [12, 24, 0, 4], [6, 12, 0, 3], [3, 6, 0, 2], [1.5, 3, 0, 2]],
  26. xl: [[40, 80, 0, 6], [20, 40, 0, 5], [10, 20, 0, 4], [5, 10, 0, 3], [2, 4, 0, 2]],
  27. '2xl': [[60, 120, 0, 7], [30, 60, 0, 6], [15, 30, 0, 5], [7.5, 15, 0, 4], [3, 6, 0, 3]],
  28. };
  29. const RING = '0 0 0 var(--smooth-ring-width, 1px) var(--smooth-ring-color)';
  30. const BORDER_LIGHT = '0 0 0 1px oklch(0 0 0 / 0.06), 0 1px 2px -1px oklch(0 0 0 / 0.06), 0 2px 4px 0 oklch(0 0 0 / 0.04)';
  31. const BORDER_DARK = '0 0 0 1px oklch(1 0 0 / 0.08)';
  32. function stack(size, withRing) {
  33. const layers = LAYERS[size].map(([y, blur, spread, alpha]) =>
  34. `0 ${y}px ${blur}px ${spread}px color-mix(in srgb, var(--smooth-shadow-color) ${alpha}%, transparent)`
  35. );
  36. if (withRing)
  37. layers.push(RING);
  38. return layers.join(', ');
  39. }
  40. module.exports = plugin(function ({ addBase, addUtilities, matchUtilities, theme }) {
  41. addBase({
  42. ':root': {
  43. '--smooth-ring-color': 'rgba(0, 0, 0, 0.05)',
  44. '--smooth-ring-width': '1px',
  45. '--smooth-shadow-border': BORDER_LIGHT,
  46. },
  47. '.light, [data-theme="light"]': {
  48. '--smooth-ring-color': 'rgba(0, 0, 0, 0.05)',
  49. '--smooth-shadow-border': BORDER_LIGHT,
  50. },
  51. '.dark, [data-theme="dark"]': {
  52. '--smooth-ring-color': 'rgba(255, 255, 255, 0.18)',
  53. '--smooth-shadow-border': BORDER_DARK,
  54. },
  55. });
  56. const utilities = {
  57. '.smooth-shadow-none': { boxShadow: 'none' },
  58. };
  59. /**
  60. * Like Tailwind's own `shadow-*`, the stack is written to `--tw-shadow` and box-shadow is composed from
  61. * the ring variables, so `ring-*` utilities on the same element add to the shadow instead of replacing it.
  62. */
  63. const BOX_SHADOW = 'var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)';
  64. for (const size of Object.keys(LAYERS)) {
  65. for (const [name, withRing] of [[`smooth-shadow-${size}`, false], [`smooth-shadow-ring-${size}`, true]]) {
  66. const value = stack(size, withRing);
  67. utilities[`.${name}`] = {
  68. '--smooth-shadow-color': 'var(--tw-shadow-color, black)',
  69. '--tw-shadow': value,
  70. '--tw-shadow-colored': value,
  71. boxShadow: BOX_SHADOW,
  72. };
  73. }
  74. }
  75. utilities['.smooth-shadow'] = utilities['.smooth-shadow-md'];
  76. utilities['.smooth-shadow-ring'] = utilities['.smooth-shadow-ring-md'];
  77. utilities['.smooth-shadow-border'] = {
  78. '--tw-shadow': 'var(--smooth-shadow-border)',
  79. '--tw-shadow-colored': 'var(--smooth-shadow-border)',
  80. boxShadow: BOX_SHADOW,
  81. };
  82. addUtilities(utilities);
  83. matchUtilities(
  84. { 'smooth-ring': (value) => ({ '--smooth-ring-color': value }) },
  85. { values: flattenColorPalette(theme('colors')), type: 'color' }
  86. );
  87. });