Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

theme-context.ts 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 = 'text-text-primary-on-surface'
  11. public backgroundButtonDefaultColorStyle = 'backgroundColor: #1C64F2'
  12. public roundedBackgroundColorStyle = 'backgroundColor: rgb(245 248 255)'
  13. public chatBubbleColorStyle = ''
  14. constructor(chatColorTheme: string | null = null, chatColorThemeInverted = false) {
  15. this.chatColorTheme = chatColorTheme
  16. this.chatColorThemeInverted = chatColorThemeInverted
  17. this.configCustomColor()
  18. this.configInvertedColor()
  19. }
  20. private configCustomColor() {
  21. if (this.chatColorTheme !== null && this.chatColorTheme !== '') {
  22. this.primaryColor = this.chatColorTheme ?? '#1C64F2'
  23. this.backgroundHeaderColorStyle = `backgroundColor: ${this.primaryColor}`
  24. this.backgroundButtonDefaultColorStyle = `backgroundColor: ${this.primaryColor}; color: ${this.colorFontOnHeaderStyle};`
  25. this.roundedBackgroundColorStyle = `backgroundColor: ${hexToRGBA(this.primaryColor, 0.05)}`
  26. this.chatBubbleColorStyle = `backgroundColor: ${hexToRGBA(this.primaryColor, 0.15)}`
  27. }
  28. }
  29. private configInvertedColor() {
  30. if (this.chatColorThemeInverted) {
  31. this.backgroundHeaderColorStyle = 'backgroundColor: #ffffff'
  32. this.colorFontOnHeaderStyle = `color: ${this.primaryColor}`
  33. this.headerBorderBottomStyle = 'borderBottom: 1px solid #ccc'
  34. this.colorPathOnHeader = this.primaryColor
  35. }
  36. }
  37. }
  38. export class ThemeBuilder {
  39. private _theme?: Theme
  40. private buildChecker = false
  41. public get theme() {
  42. if (this._theme === undefined) {
  43. this._theme = new Theme()
  44. return this._theme
  45. }
  46. else {
  47. return this._theme
  48. }
  49. }
  50. public buildTheme(chatColorTheme: string | null = null, chatColorThemeInverted = false) {
  51. if (!this.buildChecker) {
  52. this._theme = new Theme(chatColorTheme, chatColorThemeInverted)
  53. this.buildChecker = true
  54. }
  55. else {
  56. if (this.theme?.chatColorTheme !== chatColorTheme || this.theme?.chatColorThemeInverted !== chatColorThemeInverted) {
  57. this._theme = new Theme(chatColorTheme, chatColorThemeInverted)
  58. this.buildChecker = true
  59. }
  60. }
  61. }
  62. }
  63. const ThemeContext = createContext<ThemeBuilder>(new ThemeBuilder())
  64. export const useThemeContext = () => useContext(ThemeContext)