Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a theme direction #956

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ThemeProvider } from '@mui/system';
import * as React from 'react';
import 'react-quill/dist/quill.snow.css';

import { theme } from '../src/theme';
import { buildTheme } from '../src/theme';
import './global.css';

export const parameters = {
Expand Down Expand Up @@ -50,7 +50,7 @@ export const globalTypes = {
},
},
};

const theme = buildTheme()
export const decorators = [
(Story, { globals }) => {
return (
Expand Down
18 changes: 14 additions & 4 deletions src/ThemeContext/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { Direction, SxProps } from '@mui/material';
import { ThemeProvider as MuiThemeProvider } from '@mui/material';
import { SelectProps as MuiSelectProps } from '@mui/material/Select';

import { useContext, useMemo } from 'react';
import { useContext, useEffect, useMemo } from 'react';
import { createContext, useState } from 'react';

import { theme } from '../theme';
import { buildTheme } from '../theme';
import { I18nInstance } from '../types';
import LanguageSelect from './LanguageSelect';

Expand All @@ -22,6 +22,7 @@ type Props = {
languageSelectLabel?: string;
languageSelectVariant?: MuiSelectProps['variant'];
languageSelectSize?: MuiSelectProps['size'];
defaultDirection?: Direction;
};

type Context = {
Expand All @@ -48,8 +49,9 @@ const ThemeProvider = ({
languageSelectLabel,
languageSelectVariant = 'outlined',
languageSelectSize = 'small',
defaultDirection = 'ltr',
}: Props): JSX.Element => {
const [direction, setDirection] = useState<Direction>(theme.direction);
const [direction, setDirection] = useState<Direction>(defaultDirection);
const languageSelect = (
<LanguageSelect
languageSelectSx={languageSelectSx}
Expand All @@ -66,9 +68,17 @@ const ThemeProvider = ({
[direction, languageSelect],
);

useEffect(() => {
document.documentElement.setAttribute('dir', direction);
}, [direction]);

useEffect(() => {
setDirection(defaultDirection);
}, [defaultDirection]);

return (
<ThemeContext.Provider value={value}>
<MuiThemeProvider theme={{ ...theme, direction }}>
<MuiThemeProvider theme={{ ...buildTheme(direction), direction }}>
<CacheProvider value={getCacheForDirection(direction)}>
{children}
</CacheProvider>
Expand Down
14 changes: 12 additions & 2 deletions src/theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Theme, createTheme, responsiveFontSizes } from '@mui/material';
import {
Direction,
Theme,
createTheme,
responsiveFontSizes,
} from '@mui/material';
import { grey } from '@mui/material/colors';

import { Context } from '@graasp/sdk';
Expand Down Expand Up @@ -67,11 +72,14 @@ declare module '@mui/material/Typography' {

type GraaspThemeOptions = {
fontFamily?: string;
direction?: Direction;
};
export const createGraaspTheme = ({
fontFamily,
direction = 'ltr',
}: GraaspThemeOptions): Theme => {
const baseTheme = createTheme({
direction,
palette: {
action: {
active: DEFAULT_ACTIVE_ACTION_COLOR,
Expand Down Expand Up @@ -212,4 +220,6 @@ export const createGraaspTheme = ({
],
});
};
export const theme = createGraaspTheme({});

export const buildTheme = (direction: Direction = 'ltr'): Theme =>
createGraaspTheme({ direction });