createTailwindClasses.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { extend } from '@formkit/utils'
  3. import { generateClasses } from '@formkit/themes'
  4. import type {
  5. ImportGlobEagerOutput,
  6. ImportGlobEagerDefault,
  7. } from '#shared/types/utils.ts'
  8. import type {
  9. FormAppSpecificTheme,
  10. FormThemeClasses,
  11. FormThemeExtension,
  12. } from '#shared/types/form.ts'
  13. import commonClasses from '../theme/global/index.ts'
  14. const extensionsModules: ImportGlobEagerOutput<FormThemeExtension> =
  15. import.meta.glob('../theme/global/extensions/*.ts', { eager: true })
  16. const getExtensionsFromModules = (
  17. extensionsModules: ImportGlobEagerOutput<FormThemeExtension>,
  18. ) => {
  19. const extensions: FormThemeExtension[] = []
  20. Object.values(extensionsModules).forEach(
  21. (module: ImportGlobEagerDefault<FormThemeExtension>) => {
  22. const extension = module.default
  23. extensions.push(extension)
  24. },
  25. )
  26. return extensions
  27. }
  28. const createTailwindClasses = (additionalTheme: FormAppSpecificTheme = {}) => {
  29. const themeExtensions: FormThemeExtension[] = [
  30. ...getExtensionsFromModules(extensionsModules),
  31. ]
  32. if (additionalTheme.coreClasses) {
  33. themeExtensions.push(additionalTheme.coreClasses)
  34. }
  35. if (additionalTheme.extensions) {
  36. themeExtensions.push(
  37. ...getExtensionsFromModules(additionalTheme.extensions),
  38. )
  39. }
  40. let classes = commonClasses
  41. themeExtensions.forEach((extension) => {
  42. const newClasses = extension(classes)
  43. classes = extend(classes, newClasses) as FormThemeClasses
  44. })
  45. return generateClasses(classes)
  46. }
  47. export default createTailwindClasses