packages/storybook8/stories/Composites/ChatComposite/ThemesExample.stories.tsx (91 lines of code) (raw):
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { ChatComposite } from '@azure/communication-react';
import { PartialTheme, Stack } from '@fluentui/react';
import { Meta } from '@storybook/react';
import React, { useState, useEffect } from 'react';
import { compositeExperienceContainerStyle } from '../../constants';
import { defaultChatCompositeHiddenControls, controlsToAdd, getControlledTheme, ArgsFrom } from '../../controlsUtils';
import { compositeLocale } from '../../localizationUtils';
import { ContosoChatContainer, ContainerProps } from './snippets/Container.snippet';
import { ConfigHintBanner, addRemoteParticipantToThread, createThreadAndAddUser } from './snippets/Utils';
const messageArray = [
'Welcome to the theming example!',
'The ChatComposite can be themed with a Fluent UI theme, just like the base components.',
'Here, you can play around with some themes from the @fluentui/theme-samples package.',
'To build your own theme, we recommend using https://aka.ms/themedesigner',
'Have fun!'
];
const storyControls = {
userId: controlsToAdd.userId,
token: controlsToAdd.token,
remoteParticipantId: controlsToAdd.remoteParticipantUserId,
remoteParticipantToken: controlsToAdd.remoteParticipantToken,
endpointUrl: controlsToAdd.endpointUrl,
displayName: controlsToAdd.requiredDisplayName,
theme: controlsToAdd.theme,
font: controlsToAdd.font
};
const defaultControlsValues = {
displayName: 'John Smith',
theme: 'Default',
font: 'Monaco, Menlo, Consolas'
};
const ThemeStory = (args: ArgsFrom<typeof storyControls>, context): JSX.Element => {
const {
globals: { locale }
} = context;
const [containerProps, setContainerProps] = useState<ContainerProps>();
useEffect(() => {
const fetchToken = async (): Promise<void> => {
if (
args.userId &&
args.token &&
args.remoteParticipantId &&
args.remoteParticipantToken &&
args.endpointUrl &&
args.displayName
) {
const newProps = await createThreadAndAddUser(args.userId, args.token, args.endpointUrl, args.displayName);
await addRemoteParticipantToThread(
args.token,
args.remoteParticipantId,
args.remoteParticipantToken,
args.endpointUrl,
newProps.threadId,
messageArray
);
setContainerProps(newProps);
} else {
setContainerProps(undefined);
}
};
fetchToken();
}, [
args.userId,
args.token,
args.remoteParticipantId,
args.remoteParticipantToken,
args.endpointUrl,
args.displayName
]);
const theme: PartialTheme = {
...getControlledTheme(args.theme),
// The default themes exported by fluent samples, enforce Segoe UI using the `fonts` attribute in theme object.
// To override it, we need to set the `fonts` attribute to `undefined` in the theme object.
fonts: {},
defaultFontStyle: { fontFamily: args.font ?? 'Segoe UI' }
};
return (
<Stack horizontalAlign="center" verticalAlign="center" styles={compositeExperienceContainerStyle}>
{containerProps ? (
<ContosoChatContainer {...containerProps} fluentTheme={theme} locale={compositeLocale(locale)} />
) : (
<ConfigHintBanner />
)}
</Stack>
);
};
export const ThemeExample = ThemeStory.bind({});
export default {
title: 'Composites/ChatComposite/Theme Example',
component: ChatComposite,
argTypes: {
...storyControls,
// Hiding auto-generated controls
...defaultChatCompositeHiddenControls
},
parameters: {
useMaxHeightParent: true,
useMaxWidthParent: true
},
args: {
...defaultControlsValues
}
} as Meta;