Skip to content

Commit

Permalink
feat: add ability to configure font for chat application (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
denys-kolomiitsev authored Apr 30, 2024
1 parent f79904a commit 27410ce
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
4 changes: 2 additions & 2 deletions apps/chat/src/components/Markdown/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ export const CodeBlock: FC<Props> = memo(
fontSize: 14,
padding: 12,
letterSpacing: 0,
fontFamily: 'var(--font-inter)',
fontFamily: 'var(--theme-font)',
}}
className={`${isInner ? '!bg-layer-3' : '!bg-layer-1'}`}
codeTagProps={{
style: {
fontFamily: 'var(--font-inter)',
fontFamily: 'var(--theme-font)',
},
}}
>
Expand Down
12 changes: 10 additions & 2 deletions apps/chat/src/components/Settings/ThemeSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MouseEvent, useState } from 'react';
import { MouseEvent, useMemo, useState } from 'react';

import { useTranslation } from 'next-i18next';

Expand Down Expand Up @@ -27,6 +27,14 @@ export const ThemeSelect = ({
const { t } = useTranslation(Translation.Settings);
const availableThemes = useAppSelector(UISelectors.selectAvailableThemes);

const themeName = useMemo(() => {
const name = availableThemes.find(
({ id }) => id === localTheme,
)?.displayName;

return name ?? localTheme;
}, [availableThemes, localTheme]);

const onChangeHandler = (e: MouseEvent<HTMLButtonElement>) => {
onThemeChangeHandler(e.currentTarget.value);
setIsOpen(false);
Expand All @@ -45,7 +53,7 @@ export const ThemeSelect = ({
onOpenChange={setIsOpen}
trigger={
<div className="flex w-full min-w-[120px] cursor-pointer items-center justify-between gap-2 capitalize">
{localTheme}
{themeName}
<ChevronDownIcon
className={classNames(
'shrink-0 text-primary transition-all',
Expand Down
2 changes: 1 addition & 1 deletion apps/chat/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { HomeProps } from '.';
import { createStore } from '@/src/store';
import '@/src/styles/globals.css';

const inter = Inter({
export const inter = Inter({
subsets: ['latin'],
weight: 'variable',
variable: '--font-inter',
Expand Down
22 changes: 22 additions & 0 deletions apps/chat/src/pages/api/themes/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ThemesConfig } from '@/src/types/themes';

import { errorsMessages } from '@/src/constants/errors';

import { inter } from '../../_app';

import cssEscape from 'css.escape';
import fetch from 'node-fetch';

Expand Down Expand Up @@ -52,6 +54,25 @@ function generateUrlsCssVariables(
return cssContent;
}

function generateFontCssVariables(
variables: Record<string, string | undefined> | undefined,
) {
if (!variables) {
return `${inter.variable}:${inter.style.fontFamily};\n`;
}

let cssContent = '';
Object.entries(variables).forEach(([variable, value]) => {
let compiledValue = value;
if (!value || !value.length) {
compiledValue = inter.style.fontFamily;
}

cssContent += `--${cssEscape(variable)}: ${compiledValue};\n`;
});
return cssContent;
}

function wrapCssContents(wrapper: string, contents: string[]): string {
return `${wrapper} {\n ${contents.join('')}\n }\n`;
}
Expand Down Expand Up @@ -103,6 +124,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
wrapCssContents(`.${theme.id}`, [
generateColorsCssVariables(theme.colors),
generateUrlsCssVariables({ 'app-logo': theme['app-logo'] }),
generateFontCssVariables({ 'theme-font': theme['font-family'] }),
]),
),
generateUrlsCssVariables({
Expand Down
1 change: 1 addition & 0 deletions apps/chat/src/types/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface Theme {
displayName: string;
colors: Record<string, string>;
'app-logo': string;
'font-family'?: string;
id: string;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/chat/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module.exports = {
DEFAULT: '0 0 4px 0 var(--bg-blackout, #090D13B3)',
},
fontFamily: {
DEFAULT: ['var(--font-inter)'],
DEFAULT: ['var(--theme-font, var(--font-inter))'],
},
fontSize: {
xxs: '10px',
Expand Down
5 changes: 4 additions & 1 deletion docs/THEME-CUSTOMIZATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ To declare new theme you should create an object inside your themes property and
"colors": {
// Semantic colors which commonly used across entire application.
// See default configuration to check available colors
}
},
"font-family":"Inter" //Font for the theme
},
// Other themes
],
Expand All @@ -67,6 +68,7 @@ Below is the default configuration for the theme. This configuration includes th
"displayName": "Light",
"id": "light",
"app-logo": "logo.svg",
"font-family": "",
"colors": {
"bg-layer-0": "#FCFCFC",
"bg-layer-1": "#EAEDF0",
Expand Down Expand Up @@ -110,6 +112,7 @@ Below is the default configuration for the theme. This configuration includes th
"displayName": "Dark",
"id": "dark",
"app-logo": "logo-dark.svg",
"font-family": "",
"colors": {
"bg-layer-0": "#000000",
"bg-layer-1": "#090D13",
Expand Down

0 comments on commit 27410ce

Please sign in to comment.