tailwind.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 'white-85': 'rgba(255, 255, 255, 0.85)',
  58. 'black-10': 'rgba(0, 0, 0, 0.10)',
  59. 'black-20': 'rgba(0, 0, 0, 0.20)',
  60. 'black-35': 'rgba(0, 0, 0, 0.35)',
  61. 'black-50': 'rgba(0, 0, 0, 0.50)',
  62. // Color keywords.
  63. 'inherit': 'inherit',
  64. 'current': 'currentColor',
  65. 'transparent': 'transparent',
  66. /* allow system colours https://www.w3.org/TR/css-color-4/#css-system-colors */
  67. 'LinkText': 'LinkText',
  68. 'ButtonText': 'ButtonText',
  69. },
  70. fontFamily: {
  71. sans: 'var(--w-font-sans)',
  72. mono: 'var(--w-font-mono)',
  73. },
  74. fontSize,
  75. fontWeight,
  76. lineHeight,
  77. letterSpacing,
  78. borderRadius,
  79. borderWidth,
  80. boxShadow: {
  81. ...boxShadow,
  82. none: 'none',
  83. },
  84. spacing,
  85. extend: {
  86. opacity: {
  87. 15: '0.15',
  88. 85: '0.85',
  89. },
  90. outlineOffset: {
  91. inside: '-3px',
  92. },
  93. transitionProperty: {
  94. sidebar:
  95. 'inset-inline-start, padding-inline-start, width, transform, margin-top, min-height',
  96. },
  97. zIndex: {
  98. 'header': '100',
  99. 'sidebar': '110',
  100. 'sidebar-toggle': '120',
  101. 'dialog': '130',
  102. },
  103. keyframes: {
  104. 'fade-in': {
  105. '0%': { opacity: 0 },
  106. '100%': { opacity: 1 },
  107. },
  108. },
  109. animation: {
  110. 'fade-in': 'fade-in 150ms both',
  111. },
  112. },
  113. },
  114. plugins: [
  115. typeScale,
  116. vanillaRTL,
  117. scrollbarThin,
  118. /**
  119. * forced-colors media query for Windows High-Contrast mode support
  120. * See:
  121. * - https://developer.mozilla.org/en-US/docs/Web/CSS/@media/forced-colors
  122. * - https://github.com/tailwindlabs/tailwindcss/blob/v3.0.23/src/corePlugins.js#L168-L171
  123. */
  124. plugin(({ addVariant }) => {
  125. addVariant('forced-colors', '@media (forced-colors: active)');
  126. }),
  127. /**
  128. * TypeScale plugin.
  129. * This plugin generates component classes using tailwind's theme values for each object inside of the typeScale configuration.
  130. * We have the `w-` prefix added in the configuration for documentation purposes, it needs to be removed here before Tailwind adds it back.
  131. */
  132. plugin(({ addComponents, theme }) => {
  133. const scale = {};
  134. Object.entries(typeScale).forEach(([name, styles]) => {
  135. scale[`.${name.replace('w-', '')}`] = Object.fromEntries(
  136. Object.entries(styles).map(([key, value]) => [key, theme(value)]),
  137. );
  138. });
  139. addComponents(scale);
  140. }),
  141. /**
  142. * CSS Custom properties defined from our design tokens.
  143. */
  144. plugin(({ addBase }) => {
  145. addBase({
  146. ':root': {
  147. '--w-font-sans': fontFamily.sans.join(', '),
  148. '--w-font-mono': fontFamily.mono.join(', '),
  149. ...generateColorVariables(colors),
  150. },
  151. });
  152. }),
  153. ],
  154. corePlugins: {
  155. ...vanillaRTL.disabledCorePlugins,
  156. // Disable float and clear which have poor RTL support.
  157. float: false,
  158. clear: false,
  159. // Disable text-transform so we don’t rely on uppercasing text.
  160. textTransform: false,
  161. },
  162. variants: {
  163. extend: {
  164. backgroundColor: ['forced-colors'],
  165. width: ['forced-colors'],
  166. height: ['forced-colors'],
  167. },
  168. },
  169. };