tailwind.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. listStyleType,
  15. typeScale,
  16. } = require('./src/tokens/typography');
  17. const { breakpoints } = require('./src/tokens/breakpoints');
  18. const {
  19. borderRadius,
  20. borderWidth,
  21. boxShadow,
  22. } = require('./src/tokens/objectStyles');
  23. const { spacing } = require('./src/tokens/spacing');
  24. /**
  25. * Plugins
  26. */
  27. const scrollbarThin = require('./src/plugins/scrollbarThin');
  28. /**
  29. * Functions
  30. * themeColors: For converting our design tokens into a format that tailwind accepts
  31. */
  32. const themeColors = Object.fromEntries(
  33. Object.entries(colors).map(([key, hues]) => {
  34. const shades = Object.fromEntries(
  35. Object.entries(hues).map(([k, shade]) => [
  36. k,
  37. `var(${shade.cssVariable})`,
  38. ]),
  39. );
  40. return [key, shades];
  41. }),
  42. );
  43. /**
  44. * Root Tailwind config, reusable for other projects.
  45. */
  46. module.exports = {
  47. prefix: 'w-',
  48. theme: {
  49. screens: {
  50. ...breakpoints,
  51. },
  52. colors: {
  53. ...themeColors,
  54. // Fades of white and black are not customisable.
  55. 'white-15': 'rgba(255, 255, 255, 0.15)',
  56. 'white-50': 'rgba(255, 255, 255, 0.50)',
  57. 'white-80': 'rgba(255, 255, 255, 0.80)',
  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. listStyleType,
  78. letterSpacing,
  79. borderRadius,
  80. borderWidth,
  81. boxShadow: {
  82. ...boxShadow,
  83. none: 'none',
  84. },
  85. spacing: {
  86. ...spacing,
  87. 'slim-header': '50px',
  88. },
  89. extend: {
  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. /** Support for web components */
  147. ':root, :host': {
  148. '--w-font-sans': fontFamily.sans.join(', '),
  149. '--w-font-mono': fontFamily.mono.join(', '),
  150. ...generateColorVariables(colors),
  151. },
  152. });
  153. }),
  154. /** Support for aria-expanded=true variant */
  155. plugin(({ addVariant }) => {
  156. addVariant('expanded', '&[aria-expanded=true]');
  157. }),
  158. ],
  159. corePlugins: {
  160. ...vanillaRTL.disabledCorePlugins,
  161. // Disable float and clear which have poor RTL support.
  162. float: false,
  163. clear: false,
  164. // Disable text-transform so we don’t rely on uppercasing text.
  165. textTransform: false,
  166. },
  167. variants: {
  168. extend: {
  169. backgroundColor: ['forced-colors'],
  170. width: ['forced-colors'],
  171. height: ['forced-colors'],
  172. },
  173. },
  174. };