1.0.0 • Published 3 months ago

rick-ui-package v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

Package Guideline

Table of contents

ThemeSlice integration instruction

SSR

  1. Pass ThemeSlice to redux-toolkit configureStore function.

    // store/index.ts
    
    import { ThemeSlice } from '@premia/ui';
    
    const store = configureStore({
      reducer: {
        [ThemeSlice.name]: ThemeSlice.reducer,
      },
    });
  2. Now you can have access to the theme using the useTheme hook exported from @premia/ui. Also to prefill redux with the persisted value of the theme from cookies, place the useApplyPersistedTheme hook in the first decent layer of the Redux provider like in the default layout. (We are doing this as a side effect to prevent SSR hydration issues)

    // component.tsx
    
    import { useTheme } from '@premia/ui';
    // layouts/DefaultLayout.tsx
    
    import { useApplyPersistedTheme } from '@premia/ui';
    
    interface Props {
      children?: ReactNode;
    }
    
    export const DefaultLayout: React.FC<Props> = ({ children }) => {
      useApplyPersistedTheme();
    
      return <>{children}</>;
    };
  3. Update your HTML tag to contain the default theme class.

    <Html className={Theme.dark}>

SPA

  1. Do everything as SSR/SSG except that you must not use the useApplyPersistedTheme hook. instead, apply the following modifications to prefill redux with the persisted value of the theme from cookies.

    // store/index.ts
    
    import {
      applyThemeOnHTML,
      getThemeFromCookie,
      ThemeSlice,
    } from '@premia/ui';
    
    const persistedTheme = getThemeFromCookie();
    
    applyThemeOnHTML(persistedTheme);
    
    export const store = configureStore({
      reducer: {
        [ThemeSlice.name]: ThemeSlice.reducer,
      },
      preloadedState: {
        theme: {
          value: persistedTheme,
        },
      },
    });