Skip to content

Commit

Permalink
using useSyncExternalStore hook
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaish committed May 10, 2024
1 parent b1dc21d commit 16bae94
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/features/alert/alert-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Container } from '../../components/Container';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useAlertTheme } from '../../services/zustand/hooks';

export type AlertActionsProps = {
/**
Expand All @@ -20,15 +20,15 @@ export type AlertActionsProps = {
* @param props - The properties to pass to the component.
*/
export function AlertActions(props: AlertActionsProps) {
const store = useStore();
const alertTheme = useAlertTheme();

return (
<Container
flexBasis={{ base: 0, lg: '215px' }}
flexDirection={{ base: 'row', lg: 'column' }}
justifyContent={{ base: 'left', lg: 'center' }}
gap={2}
theme={store.alertTheme}
theme={alertTheme}
userDefinedStyle={props.containerStyle}
>
{props.children}
Expand Down
4 changes: 3 additions & 1 deletion src/features/alert/alert-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { Button, ButtonProps } from '../../components/Button';
import { ThemeStyles } from '../../constants/types';
import { useAlertTheme } from '../../services/zustand/hooks';
import { useStore } from '../../services/zustand/store';

export interface AlertButtonProps extends Omit<ButtonProps, 'theme'> {
Expand Down Expand Up @@ -30,6 +31,7 @@ export function AlertButton(props: AlertButtonProps) {
...rest
} = props;

const alertTheme = useAlertTheme();
const store = useStore();

/**
Expand Down Expand Up @@ -68,7 +70,7 @@ export function AlertButton(props: AlertButtonProps) {
{...rest}
containerStyle={getContainerStyle()}
onClick={handleOnClick}
theme={store.alertTheme}
theme={alertTheme}
variant={variant}
/>
);
Expand Down
6 changes: 3 additions & 3 deletions src/features/alert/alert-content.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Container } from '../../components/Container';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useAlertTheme } from '../../services/zustand/hooks';

export type AlertContentProps = {
/**
Expand All @@ -20,13 +20,13 @@ export type AlertContentProps = {
* @param props - The properties to pass to the component.
*/
export function AlertContent(props: AlertContentProps) {
const store = useStore();
const alertTheme = useAlertTheme();

return (
<Container
flexDirection="column"
flexGrow={1}
theme={store.alertTheme}
theme={alertTheme}
userDefinedStyle={props.containerStyle}
>
{props.children}
Expand Down
8 changes: 4 additions & 4 deletions src/features/alert/alert-description.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Container } from '../../components/Container';
import { Text } from '../../components/Text';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useAlertTheme } from '../../services/zustand/hooks';

export type AlertDescriptionProps = {
/**
Expand All @@ -26,13 +26,13 @@ export type AlertDescriptionProps = {
* @param props - The properties to pass to the component.
*/
export function AlertDescription(props: AlertDescriptionProps) {
const store = useStore();
const alertTheme = useAlertTheme();

return (
<Container theme={store.alertTheme} userDefinedStyle={props.containerStyle}>
<Container theme={alertTheme} userDefinedStyle={props.containerStyle}>
<Text
fontSize={{ base: 'sm', lg: 'md' }}
theme={store.alertTheme}
theme={alertTheme}
userDefinedStyle={props.textStyle}
>
{props.text}
Expand Down
4 changes: 3 additions & 1 deletion src/features/alert/alert-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Container } from '../../components/Container';
import { MotionBox } from '../../components/MotionBox';
import { LocalStorageKeys } from '../../constants/settings';
import { Theme, ThemeStyles } from '../../constants/types';
import { useAlertDismissed } from '../../services/zustand/hooks';
import { useStore } from '../../services/zustand/store';

export type AlertRootRef = {
Expand Down Expand Up @@ -78,6 +79,7 @@ export const AlertRoot = React.forwardRef<AlertRootRef, AlertRootProps>(
},
ref
) => {
const alertDismissed = useAlertDismissed();
const store = useStore();

// Update the stored theme when the theme prop changes
Expand Down Expand Up @@ -185,7 +187,7 @@ export const AlertRoot = React.forwardRef<AlertRootRef, AlertRootProps>(
return (
<ChakraProvider>
<AnimatePresence>
{!store.alertDismissed && (
{!alertDismissed && (
<MotionBox
animate={enterExitAnimationEnabled ? 'visible' : 'disabled'}
bottom={6}
Expand Down
8 changes: 4 additions & 4 deletions src/features/alert/alert-title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Container } from '../../components/Container';
import { Text } from '../../components/Text';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useAlertTheme } from '../../services/zustand/hooks';

export type AlertTitleProps = {
/**
Expand All @@ -26,14 +26,14 @@ export type AlertTitleProps = {
* @param props - The properties to pass to the component.
*/
export function AlertTitle(props: AlertTitleProps) {
const store = useStore();
const alertTheme = useAlertTheme();

return (
<Container theme={store.alertTheme} userDefinedStyle={props.containerStyle}>
<Container theme={alertTheme} userDefinedStyle={props.containerStyle}>
<Text
fontSize={{ base: 'md', lg: 'lg' }}
fontWeight="semibold"
theme={store.alertTheme}
theme={alertTheme}
userDefinedStyle={props.textStyle}
>
{props.text}
Expand Down
8 changes: 4 additions & 4 deletions src/features/modal/modal-body.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModalBody as ChakraUiModalBody } from '@chakra-ui/react';
import React from 'react';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useModalTheme } from '../../services/zustand/hooks';

export type ModalBodyProps = {
/**
Expand All @@ -20,21 +20,21 @@ export type ModalBodyProps = {
* @param props - The properties to pass to the component.
*/
export function ModalBody(props: ModalBodyProps) {
const store = useStore();
const modalTheme = useModalTheme();

/**
* Gets the styles for the modal body container based on the theme.
* @returns The styles for the modal body container.
*/
function getContainerStyle(): React.CSSProperties | undefined {
let tempStyle: React.CSSProperties = {
...defaultContainerStyle[store.modalTheme]
...defaultContainerStyle[modalTheme]
};

if (props.containerStyle) {
tempStyle = {
...tempStyle,
...(props.containerStyle[store.modalTheme] ?? {})
...(props.containerStyle[modalTheme] ?? {})
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/features/modal/modal-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { Button, ButtonProps } from '../../components/Button';
import { ThemeStyles } from '../../constants/types';
import { useModalTheme } from '../../services/zustand/hooks';
import { useStore } from '../../services/zustand/store';

export interface ModalButtonProps extends Omit<ButtonProps, 'theme'> {
Expand Down Expand Up @@ -29,7 +30,7 @@ export function ModalButton(props: ModalButtonProps) {
variant = 'regular',
...rest
} = props;

const modalTheme = useModalTheme();
const store = useStore();

/**
Expand Down Expand Up @@ -67,7 +68,7 @@ export function ModalButton(props: ModalButtonProps) {
{...rest}
containerStyle={getContainerStyle()}
onClick={handleOnClick}
theme={store.modalTheme}
theme={modalTheme}
variant={variant}
/>
);
Expand Down
12 changes: 6 additions & 6 deletions src/features/modal/modal-cookie-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Flex, Switch } from '@chakra-ui/react';
import React from 'react';
import { Text } from '../../components/Text';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useModalTheme } from '../../services/zustand/hooks';

export type ModalCookieActionProps = {
/**
Expand Down Expand Up @@ -73,7 +73,7 @@ export function ModalCookieAction({
switchToggledOnColor = '#0082ba',
...props
}: ModalCookieActionProps) {
const store = useStore();
const modalTheme = useModalTheme();

const [isSwitchOn, setIsSwitchOn] = React.useState(false);

Expand All @@ -83,13 +83,13 @@ export function ModalCookieAction({
*/
function getContainerStyle(): React.CSSProperties | undefined {
let tempStyle: React.CSSProperties = {
...defaultContainerStyle[store.modalTheme]
...defaultContainerStyle[modalTheme]
};

if (props.containerStyle) {
tempStyle = {
...tempStyle,
...(props.containerStyle[store.modalTheme] ?? {})
...(props.containerStyle[modalTheme] ?? {})
};
}

Expand Down Expand Up @@ -128,7 +128,7 @@ export function ModalCookieAction({
<Text
fontSize={{ base: 'sm', lg: 'md' }}
fontWeight="semibold"
theme={store.modalTheme}
theme={modalTheme}
userDefinedStyle={props.titleStyle}
>
{props.title}
Expand All @@ -138,7 +138,7 @@ export function ModalCookieAction({
<Text
fontSize={{ base: 'xs', lg: 'sm' }}
userDefinedStyle={props.descriptionStyle}
theme={store.modalTheme}
theme={modalTheme}
>
{props.description}
</Text>
Expand Down
6 changes: 3 additions & 3 deletions src/features/modal/modal-cookie-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Container } from '../../components/Container';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useModalTheme } from '../../services/zustand/hooks';

export type ModalCookieActionsProps = {
/**
Expand All @@ -21,15 +21,15 @@ export type ModalCookieActionsProps = {
* @param props - The properties to pass to the component.
*/
export function ModalCookieActions(props: ModalCookieActionsProps) {
const store = useStore();
const modalTheme = useModalTheme();

return (
<Container
flexDirection="column"
gap={4}
mb={4}
mt={8}
theme={store.modalTheme}
theme={modalTheme}
userDefinedStyle={props.containerStyle}
>
{props.children}
Expand Down
6 changes: 3 additions & 3 deletions src/features/modal/modal-cta-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Container } from '../../components/Container';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useModalTheme } from '../../services/zustand/hooks';

export type ModalCtaActionsProps = {
/**
Expand All @@ -21,13 +21,13 @@ export type ModalCtaActionsProps = {
* @param props - The properties to pass to the component.
*/
export function ModalCtaActions(props: ModalCtaActionsProps) {
const store = useStore();
const modalTheme = useModalTheme();

return (
<Container
gap={2}
my={2}
theme={store.modalTheme}
theme={modalTheme}
userDefinedStyle={props.containerStyle}
>
{props.children}
Expand Down
8 changes: 4 additions & 4 deletions src/features/modal/modal-footer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModalFooter as ChakraUiModalFooter } from '@chakra-ui/react';
import React from 'react';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useModalTheme } from '../../services/zustand/hooks';

export type ModalFooterProps = {
/**
Expand All @@ -20,21 +20,21 @@ export type ModalFooterProps = {
* @param props - The properties to pass to the component.
*/
export function ModalFooter(props: ModalFooterProps) {
const store = useStore();
const modalTheme = useModalTheme();

/**
* Gets the styles for the modal footer container based on the theme.
* @returns The styles for the modal footer container.
*/
function getContainerStyle(): React.CSSProperties | undefined {
let tempStyle: React.CSSProperties = {
...defaultContainerStyle[store.modalTheme]
...defaultContainerStyle[modalTheme]
};

if (props.containerStyle) {
tempStyle = {
...tempStyle,
...(props.containerStyle[store.modalTheme] ?? {})
...(props.containerStyle[modalTheme] ?? {})
};
}

Expand Down
12 changes: 6 additions & 6 deletions src/features/modal/modal-header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModalHeader as ChakraUiModalHeader } from '@chakra-ui/react';
import React from 'react';
import { ThemeStyles } from '../../constants/types';
import { useStore } from '../../services/zustand/store';
import { useModalTheme } from '../../services/zustand/hooks';

export type ModalHeaderProps = {
/**
Expand All @@ -25,29 +25,29 @@ export type ModalHeaderProps = {
* @param props - The properties to pass to the component.
*/
export function ModalHeader(props: ModalHeaderProps) {
const store = useStore();
const modalTheme = useModalTheme();

/**
* Gets the styles for the modal header container and text based on the theme.
* @returns The styles for the modal header container and text.
*/
function getContainerAndTextStyle(): React.CSSProperties | undefined {
let tempStyle: React.CSSProperties = {
...defaultContainerStyle[store.modalTheme],
...defaultTextStyle[store.modalTheme]
...defaultContainerStyle[modalTheme],
...defaultTextStyle[modalTheme]
};

if (props.containerStyle) {
tempStyle = {
...tempStyle,
...(props.containerStyle[store.modalTheme] ?? {})
...(props.containerStyle[modalTheme] ?? {})
};
}

if (props.textStyle) {
tempStyle = {
...tempStyle,
...(props.textStyle[store.modalTheme] ?? {})
...(props.textStyle[modalTheme] ?? {})
};
}

Expand Down
Loading

0 comments on commit 16bae94

Please sign in to comment.