2
0

🐛 (js) Fix default theme values css variables

Closes #1031
This commit is contained in:
Baptiste Arnaud
2023-11-14 16:45:25 +01:00
parent a1d7415227
commit fd00b6fdd5
4 changed files with 67 additions and 61 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/js", "name": "@typebot.io/js",
"version": "0.2.17", "version": "0.2.18",
"description": "Javascript library to display typebots on your website", "description": "Javascript library to display typebots on your website",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -54,98 +54,104 @@ export const setCssVariablesValue = (
if (!theme) return if (!theme) return
const documentStyle = container?.style const documentStyle = container?.style
if (!documentStyle) return if (!documentStyle) return
if (theme.general) setGeneralTheme(theme.general, documentStyle) setGeneralTheme(theme.general ?? defaultTheme.general, documentStyle)
if (theme.chat) setChatTheme(theme.chat, documentStyle) setChatTheme(theme.chat ?? defaultTheme.chat, documentStyle)
} }
const setGeneralTheme = ( const setGeneralTheme = (
generalTheme: GeneralTheme, generalTheme: GeneralTheme,
documentStyle: CSSStyleDeclaration documentStyle: CSSStyleDeclaration
) => { ) => {
const { background, font } = generalTheme setTypebotBackground(
if (background) setTypebotBackground(background, documentStyle) generalTheme.background ?? defaultTheme.general.background,
if (font) documentStyle.setProperty(cssVariableNames.general.fontFamily, font) documentStyle
)
documentStyle.setProperty(
cssVariableNames.general.fontFamily,
generalTheme.font ?? defaultTheme.general.font
)
} }
const setChatTheme = ( const setChatTheme = (
chatTheme: ChatTheme, chatTheme: ChatTheme,
documentStyle: CSSStyleDeclaration documentStyle: CSSStyleDeclaration
) => { ) => {
const { hostBubbles, guestBubbles, buttons, inputs, roundness } = chatTheme setHostBubbles(
if (hostBubbles) setHostBubbles(hostBubbles, documentStyle) chatTheme.hostBubbles ?? defaultTheme.chat.hostBubbles,
if (guestBubbles) setGuestBubbles(guestBubbles, documentStyle) documentStyle
if (buttons) setButtons(buttons, documentStyle) )
if (inputs) setInputs(inputs, documentStyle) setGuestBubbles(
if (roundness) setRoundness(roundness, documentStyle) chatTheme.guestBubbles ?? defaultTheme.chat.guestBubbles,
documentStyle
)
setButtons(chatTheme.buttons ?? defaultTheme.chat.buttons, documentStyle)
setInputs(chatTheme.inputs ?? defaultTheme.chat.inputs, documentStyle)
setRoundness(
chatTheme.roundness ?? defaultTheme.chat.roundness,
documentStyle
)
} }
const setHostBubbles = ( const setHostBubbles = (
hostBubbles: ContainerColors, hostBubbles: ContainerColors,
documentStyle: CSSStyleDeclaration documentStyle: CSSStyleDeclaration
) => { ) => {
if (hostBubbles.backgroundColor) documentStyle.setProperty(
documentStyle.setProperty( cssVariableNames.chat.hostBubbles.bgColor,
cssVariableNames.chat.hostBubbles.bgColor, hostBubbles.backgroundColor ?? defaultTheme.chat.hostBubbles.backgroundColor
hostBubbles.backgroundColor )
) documentStyle.setProperty(
if (hostBubbles.color) cssVariableNames.chat.hostBubbles.color,
documentStyle.setProperty( hostBubbles.color ?? defaultTheme.chat.hostBubbles.color
cssVariableNames.chat.hostBubbles.color, )
hostBubbles.color
)
} }
const setGuestBubbles = ( const setGuestBubbles = (
guestBubbles: ContainerColors, guestBubbles: ContainerColors,
documentStyle: CSSStyleDeclaration documentStyle: CSSStyleDeclaration
) => { ) => {
if (guestBubbles.backgroundColor) documentStyle.setProperty(
documentStyle.setProperty( cssVariableNames.chat.guestBubbles.bgColor,
cssVariableNames.chat.guestBubbles.bgColor, guestBubbles.backgroundColor ??
guestBubbles.backgroundColor defaultTheme.chat.guestBubbles.backgroundColor
) )
if (guestBubbles.color) documentStyle.setProperty(
documentStyle.setProperty( cssVariableNames.chat.guestBubbles.color,
cssVariableNames.chat.guestBubbles.color, guestBubbles.color ?? defaultTheme.chat.guestBubbles.color
guestBubbles.color )
)
} }
const setButtons = ( const setButtons = (
buttons: ContainerColors, buttons: ContainerColors,
documentStyle: CSSStyleDeclaration documentStyle: CSSStyleDeclaration
) => { ) => {
if (buttons.backgroundColor) { const bgColor =
documentStyle.setProperty( buttons.backgroundColor ?? defaultTheme.chat.buttons.backgroundColor
cssVariableNames.chat.buttons.bgColor, documentStyle.setProperty(cssVariableNames.chat.buttons.bgColor, bgColor)
buttons.backgroundColor documentStyle.setProperty(
) cssVariableNames.chat.buttons.bgColorRgb,
documentStyle.setProperty( hexToRgb(bgColor).join(', ')
cssVariableNames.chat.buttons.bgColorRgb, )
hexToRgb(buttons.backgroundColor).join(', ')
)
}
if (buttons.color) documentStyle.setProperty(
documentStyle.setProperty( cssVariableNames.chat.buttons.color,
cssVariableNames.chat.buttons.color, buttons.color ?? defaultTheme.chat.buttons.color
buttons.color )
)
} }
const setInputs = (inputs: InputColors, documentStyle: CSSStyleDeclaration) => { const setInputs = (inputs: InputColors, documentStyle: CSSStyleDeclaration) => {
if (inputs.backgroundColor) documentStyle.setProperty(
documentStyle.setProperty( cssVariableNames.chat.inputs.bgColor,
cssVariableNames.chat.inputs.bgColor, inputs.backgroundColor ?? defaultTheme.chat.inputs.backgroundColor
inputs.backgroundColor )
) documentStyle.setProperty(
if (inputs.color) cssVariableNames.chat.inputs.color,
documentStyle.setProperty(cssVariableNames.chat.inputs.color, inputs.color) inputs.color ?? defaultTheme.chat.inputs.color
if (inputs.placeholderColor) )
documentStyle.setProperty( documentStyle.setProperty(
cssVariableNames.chat.inputs.placeholderColor, cssVariableNames.chat.inputs.placeholderColor,
inputs.placeholderColor inputs.placeholderColor ?? defaultTheme.chat.inputs.placeholderColor
) )
} }
const setTypebotBackground = ( const setTypebotBackground = (

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/nextjs", "name": "@typebot.io/nextjs",
"version": "0.2.17", "version": "0.2.18",
"description": "Convenient library to display typebots on your Next.js website", "description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/react", "name": "@typebot.io/react",
"version": "0.2.17", "version": "0.2.18",
"description": "Convenient library to display typebots on your React app", "description": "Convenient library to display typebots on your React app",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",