theme-context.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { createContext, useContext } from 'use-context-selector'
  2. import { hexToRGBA } from './utils'
  3. export class Theme {
  4. public chatColorTheme: string | null
  5. public chatColorThemeInverted: boolean
  6. public primaryColor = '#1C64F2'
  7. public backgroundHeaderColorStyle = 'backgroundImage: linear-gradient(to right, #2563eb, #0ea5e9)'
  8. public headerBorderBottomStyle = ''
  9. public colorFontOnHeaderStyle = 'color: white'
  10. public colorPathOnHeader = 'white'
  11. public backgroundButtonDefaultColorStyle = 'backgroundColor: #1C64F2'
  12. public roundedBackgroundColorStyle = 'backgroundColor: rgb(245 248 255)'
  13. public chatBubbleColorStyle = 'backgroundColor: rgb(225 239 254)'
  14. public chatBubbleColor = 'rgb(225 239 254)'
  15. constructor(chatColorTheme: string | null = null, chatColorThemeInverted = false) {
  16. this.chatColorTheme = chatColorTheme
  17. this.chatColorThemeInverted = chatColorThemeInverted
  18. this.configCustomColor()
  19. this.configInvertedColor()
  20. }
  21. private configCustomColor() {
  22. if (this.chatColorTheme !== null && this.chatColorTheme !== '') {
  23. this.primaryColor = this.chatColorTheme ?? '#1C64F2'
  24. this.backgroundHeaderColorStyle = `backgroundColor: ${this.primaryColor}`
  25. this.backgroundButtonDefaultColorStyle = `backgroundColor: ${this.primaryColor}; color: ${this.colorFontOnHeaderStyle};`
  26. this.roundedBackgroundColorStyle = `backgroundColor: ${hexToRGBA(this.primaryColor, 0.05)}`
  27. this.chatBubbleColorStyle = `backgroundColor: ${hexToRGBA(this.primaryColor, 0.15)}`
  28. this.chatBubbleColor = `${hexToRGBA(this.primaryColor, 0.15)}`
  29. }
  30. }
  31. private configInvertedColor() {
  32. if (this.chatColorThemeInverted) {
  33. this.backgroundHeaderColorStyle = 'backgroundColor: #ffffff'
  34. this.colorFontOnHeaderStyle = `color: ${this.primaryColor}`
  35. this.headerBorderBottomStyle = 'borderBottom: 1px solid #ccc'
  36. this.colorPathOnHeader = this.primaryColor
  37. }
  38. }
  39. }
  40. export class ThemeBuilder {
  41. private _theme?: Theme
  42. private buildChecker = false
  43. public get theme() {
  44. if (this._theme === undefined)
  45. throw new Error('The theme should be built first and then accessed')
  46. else
  47. return this._theme
  48. }
  49. public buildTheme(chatColorTheme: string | null = null, chatColorThemeInverted = false) {
  50. if (!this.buildChecker) {
  51. this._theme = new Theme(chatColorTheme, chatColorThemeInverted)
  52. this.buildChecker = true
  53. }
  54. else {
  55. if (this.theme?.chatColorTheme !== chatColorTheme || this.theme?.chatColorThemeInverted !== chatColorThemeInverted) {
  56. this._theme = new Theme(chatColorTheme, chatColorThemeInverted)
  57. this.buildChecker = true
  58. }
  59. }
  60. }
  61. }
  62. const ThemeContext = createContext<ThemeBuilder>(new ThemeBuilder())
  63. export const useThemeContext = () => useContext(ThemeContext)