Skip to content

Commit

Permalink
WIP: create ThemeProvider from styled-components
Browse files Browse the repository at this point in the history
  • Loading branch information
Iru89 committed Jan 10, 2025
1 parent 6bc71bf commit 43373ab
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const DefaultHeader = () => {
return (
<HeaderContainer
role={ROLES.HEADER}
color={color}
// color={color}
style={{ ...getThemeProperty(WEBCHAT.CUSTOM_PROPERTIES.headerStyle) }}
>
{headerImage && (
Expand Down
5 changes: 3 additions & 2 deletions packages/botonic-react/src/webchat/header/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { COLORS, WEBCHAT } from '../../constants'

export const HeaderContainer = styled.div`
display: flex;
background: linear-gradient(
/* background: linear-gradient(
90deg,
${COLORS.BLEACHED_CEDAR_PURPLE} 0%,
${props => props.color} 100%
);
); */
background: ${props => props.theme.colors?.primary};
border-radius: ${WEBCHAT.DEFAULTS.BORDER_RADIUS_TOP_CORNERS};
z-index: 2;
height: inherit;
Expand Down
61 changes: 61 additions & 0 deletions packages/botonic-react/src/webchat/theme/theme.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'styled-components'

// With this declaration, we can use the theme prop in styled components with TyeScript
declare module 'styled-components' {
export interface DefaultTheme {
colors?: {
primary: string
primaryHover: string
primaryDisabled: string
secondary: string
secondaryHover: string
secondaryDisabled: string
}
// style: {
// width: number
// height: number
// borderRadius?: string
// fontFamily?: string
// fontSize?: string
// position: 'fixed' | 'absolute'
// right: number
// bottom: number
// }
// mobileBreakpoint?: number
// mobileStyle?: {
// width?: string
// height?: string
// borderRadius?: string
// fontSize?: string
// position: 'fixed' | 'absolute'
// right: number
// bottom: number
// }
// header?: {
// borderRadius?: string
// image?: string
// color?: string
// }
// message?: {
// bot?: {
// backgroundColor?: string
// image?: string
// imagePosition: 'next' | 'footer'
// }
// agent?: {
// backgroundColor?: string
// image?: string
// imagePosition: 'next' | 'footer'
// }
// user?: {
// backgroundColor?: string
// }
// timestamps?: {
// withImage?: boolean
// format: () => string
// style?: any
// enable?: boolean
// }
// }
}
}
6 changes: 6 additions & 0 deletions packages/botonic-react/src/webchat/theme/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import {
// Functionalities should have the functionalities as enable or disable diferents behaviors
export interface ThemeProps {
style?: any
// style:{
// width: number
// height: number
// borderRadius?: string
// fontFamily?: string
// }
coverComponent?: CoverComponentOptions
mobileBreakpoint?: number
mobileStyle?: any
Expand Down
118 changes: 60 additions & 58 deletions packages/botonic-react/src/webchat/webchat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import React, {
useRef,
useState,
} from 'react'
import { StyleSheetManager } from 'styled-components'
import { StyleSheetManager, ThemeProvider } from 'styled-components'
import { v7 as uuidv7 } from 'uuid'

import {
Expand Down Expand Up @@ -706,63 +706,65 @@ const Webchat = forwardRef<WebchatRef | null, WebchatProps>((props, ref) => {
scrollableMessagesListRef,
}}
>
{!webchatState.isWebchatOpen && <TriggerButton />}

{webchatState.isWebchatOpen && (
<StyledWebchat
id={BotonicContainerId.Webchat}
ref={webchatRef}
// TODO: Distinguish between multiple instances of webchat, e.g. `${uniqueId}-botonic-webchat`
role={ROLES.WEBCHAT}
width={webchatState.theme.style.width}
height={webchatState.theme.style.height}
style={{
...webchatState.theme.style,
...mobileStyle,
}}
>
<WebchatHeader ref={headerRef} />

{webchatState.isCoverComponentOpen ? (
<CoverComponent
component={coverComponent}
componentProps={coverComponentProps}
/>
) : (
<>
{webchatState.error.message && (
<ErrorMessageContainer>
<ErrorMessage>{webchatState.error.message}</ErrorMessage>
</ErrorMessageContainer>
)}

<ChatArea />

{webchatState.isPersistentMenuOpen && (
<DarkenBackground component={persistentMenu()} />
)}

{!webchatState.handoff && userInputEnabled && (
<InputPanel
persistentMenu={props.persistentMenu}
enableEmojiPicker={props.enableEmojiPicker}
enableAttachments={props.enableAttachments}
handleAttachment={handleAttachment}
textareaRef={textareaRef}
host={host}
onUserInput={props.onUserInput}
/>
)}

{webchatState.webview && webchatWebview()}

{webchatState.isCustomComponentRendered &&
customComponent &&
_renderCustomComponent()}
</>
)}
</StyledWebchat>
)}
<ThemeProvider theme={webchatState.theme}>
{!webchatState.isWebchatOpen && <TriggerButton />}

{webchatState.isWebchatOpen && (
<StyledWebchat
id={BotonicContainerId.Webchat}
ref={webchatRef}
// TODO: Distinguish between multiple instances of webchat, e.g. `${uniqueId}-botonic-webchat`
role={ROLES.WEBCHAT}
width={webchatState.theme.style.width}
height={webchatState.theme.style.height}
style={{
...webchatState.theme.style,
...mobileStyle,
}}
>
<WebchatHeader ref={headerRef} />

{webchatState.isCoverComponentOpen ? (
<CoverComponent
component={coverComponent}
componentProps={coverComponentProps}
/>
) : (
<>
{webchatState.error.message && (
<ErrorMessageContainer>
<ErrorMessage>{webchatState.error.message}</ErrorMessage>
</ErrorMessageContainer>
)}

<ChatArea />

{webchatState.isPersistentMenuOpen && (
<DarkenBackground component={persistentMenu()} />
)}

{!webchatState.handoff && userInputEnabled && (
<InputPanel
persistentMenu={props.persistentMenu}
enableEmojiPicker={props.enableEmojiPicker}
enableAttachments={props.enableAttachments}
handleAttachment={handleAttachment}
textareaRef={textareaRef}
host={host}
onUserInput={props.onUserInput}
/>
)}

{webchatState.webview && webchatWebview()}

{webchatState.isCustomComponentRendered &&
customComponent &&
_renderCustomComponent()}
</>
)}
</StyledWebchat>
)}
</ThemeProvider>
</WebchatContext.Provider>
)

Expand Down

0 comments on commit 43373ab

Please sign in to comment.