tailwind.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const plugin = require('tailwindcss/plugin');
  2. const vanillaRTL = require('tailwindcss-vanilla-rtl');
  3. /**
  4. * Design Tokens
  5. */
  6. const colors = require('./src/tokens/colors');
  7. const { generateColorVariables } = require('./src/tokens/colorVariables');
  8. const {
  9. fontFamily,
  10. fontSize,
  11. fontWeight,
  12. letterSpacing,
  13. lineHeight,
  14. typeScale,
  15. } = require('./src/tokens/typography');
  16. const { breakpoints } = require('./src/tokens/breakpoints');
  17. const {
  18. borderRadius,
  19. borderWidth,
  20. boxShadow,
  21. } = require('./src/tokens/objectStyles');
  22. const { spacing } = require('./src/tokens/spacing');
  23. /**
  24. * Plugins
  25. */
  26. const scrollbarThin = require('./src/plugins/scrollbarThin');
  27. /**
  28. * Functions
  29. * themeColors: For converting our design tokens into a format that tailwind accepts
  30. */
  31. const themeColors = Object.fromEntries(
  32. Object.entries(colors).map(([key, hues]) => {
  33. const shades = Object.fromEntries(
  34. Object.entries(hues).map(([k, shade]) => [
  35. k,
  36. `var(${shade.cssVariable})`,
  37. ]),
  38. );
  39. return [key, shades];
  40. }),
  41. );
  42. /**
  43. * Root Tailwind config, reusable for other projects.
  44. */
  45. module.exports = {
  46. prefix: 'w-',
  47. theme: {
  48. screens: {
  49. ...breakpoints,
  50. },
  51. colors: {
  52. ...themeColors,
  53. // Fades of white and black are not customisable.
  54. 'white-15': 'rgba(255, 255, 255, 0.15)',
  55. 'white-50': 'rgba(255, 255, 255, 0.50)',
  56. 'white-80': 'rgba(255, 255, 255, 0.80)',
  57. 'black-10': 'rgba(0, 0, 0, 0.10)',
  58. 'black-20': 'rgba(0, 0, 0, 0.20)',
  59. 'black-35': 'rgba(0, 0, 0, 0.35)',
  60. 'black-50': 'rgba(0, 0, 0, 0.50)',
  61. // Color keywords.
  62. 'inherit': 'inherit',
  63. 'current': 'currentColor',
  64. 'transparent': 'transparent',
  65. /* allow system colours https://www.w3.org/TR/css-color-4/#css-system-colors */
  66. 'LinkText': 'LinkText',
  67. 'ButtonText': 'ButtonText',
  68. },
  69. fontFamily: {
  70. sans: 'var(--w-font-sans)',
  71. mono: 'var(--w-font-mono)',
  72. },
  73. fontSize,
  74. fontWeight,
  75. lineHeight,
  76. letterSpacing,
  77. borderRadius,
  78. borderWidth,
  79. boxShadow: {
  80. ...boxShadow,
  81. none: 'none',
  82. },
  83. spacing: {
  84. ...spacing,
  85. 'slim-header': '50px',
  86. },
  87. extend: {
  88. outlineOffset: {
  89. inside: '-3px',
  90. },
  91. transitionProperty: {
  92. sidebar:
  93. 'inset-inline-start, padding-inline-start, width, transform, margin-top, min-height',
  94. },
  95. zIndex: {
  96. 'header': '100',
  97. 'sidebar': '110',
  98. 'sidebar-toggle': '120',
  99. 'dialog': '130',
  100. },
  101. keyframes: {
  102. 'fade-in': {
  103. '0%': { opacity: 0 },
  104. '100%': { opacity: 1 },
  105. },
  106. },
  107. animation: {
  108. 'fade-in': 'fade-in 150ms both',
  109. },
  110. },
  111. },
  112. plugins: [
  113. typeScale,
  114. vanillaRTL,
  115. scrollbarThin,
  116. /**
  117. * forced-colors media query for Windows High-Contrast mode support
  118. * See:
  119. * - https://developer.mozilla.org/en-US/docs/Web/CSS/@media/forced-colors
  120. * - https://github.com/tailwindlabs/tailwindcss/blob/v3.0.23/src/corePlugins.js#L168-L171
  121. */
  122. plugin(({ addVariant }) => {
  123. addVariant('forced-colors', '@media (forced-colors: active)');
  124. }),
  125. /**
  126. * TypeScale plugin.
  127. * This plugin generates component classes using tailwind's theme values for each object inside of the typeScale configuration.
  128. * We have the `w-` prefix added in the configuration for documentation purposes, it needs to be removed here before Tailwind adds it back.
  129. */
  130. plugin(({ addComponents, theme }) => {
  131. const scale = {};
  132. Object.entries(typeScale).forEach(([name, styles]) => {
  133. scale[`.${name.replace('w-', '')}`] = Object.fromEntries(
  134. Object.entries(styles).map(([key, value]) => [key, theme(value)]),
  135. );
  136. });
  137. addComponents(scale);
  138. }),
  139. /**
  140. * CSS Custom properties defined from our design tokens.
  141. */
  142. plugin(({ addBase }) => {
  143. addBase({
  144. ':root': {
  145. '--w-font-sans': fontFamily.sans.join(', '),
  146. '--w-font-mono': fontFamily.mono.join(', '),
  147. ...generateColorVariables(colors),
  148. },
  149. });
  150. }),
  151. /** Support for aria-expanded=true variant */
  152. plugin(({ addVariant }) => {
  153. addVariant('expanded', '&[aria-expanded=true]');
  154. }),
  155. ],
  156. corePlugins: {
  157. ...vanillaRTL.disabledCorePlugins,
  158. // Disable float and clear which have poor RTL support.
  159. float: false,
  160. clear: false,
  161. // Disable text-transform so we don’t rely on uppercasing text.
  162. textTransform: false,
  163. },
  164. variants: {
  165. extend: {
  166. backgroundColor: ['forced-colors'],
  167. width: ['forced-colors'],
  168. height: ['forced-colors'],
  169. },
  170. },
  171. };