Skip to content

Commit

Permalink
chore: Applies next/dynamic for RegionModal (#2523)
Browse files Browse the repository at this point in the history
## What's the purpose of this pull request?

Loads `RegionModal` using dynamic import.

## How it works?

We are aiming to optimize the performance loading components only when
they are needed, rather than at the initial page load.

## How to test it?

- Click on `Set your location` button, the `RegionModal` should appear
as expected.

<img width="300" alt="image"
src="https://github.com/user-attachments/assets/a7644c15-66c1-4c14-be83-06e2b4341166">
  • Loading branch information
hellofanny authored Oct 24, 2024
1 parent b2aecbd commit 992cab3
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions packages/core/src/components/region/RegionModal/RegionModal.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import {
Icon,
RegionModal as UIRegionModal,
RegionModalProps as UIRegionModalProps,
useUI,
} from '@faststore/ui'
import { Suspense, useRef, useState } from 'react'
import { useRef, useState } from 'react'

import { sessionStore, useSession, validateSession } from 'src/sdk/session'

import dynamic from 'next/dynamic'
import styles from './section.module.scss'

const UIRegionModal = dynamic<UIRegionModalProps>(
() =>
import(/* webpackChunkName: "UIRegionModal" */ '@faststore/ui').then(
(mod) => mod.RegionModal
),
{ ssr: false }
)

interface RegionModalProps {
title?: UIRegionModalProps['title']
description?: UIRegionModalProps['description']
Expand Down Expand Up @@ -88,28 +96,26 @@ function RegionModal({
return (
<>
{displayModal && (
<Suspense fallback={null}>
<UIRegionModal
title={title}
description={description}
overlayProps={{
className: `section ${styles.section} section-region-modal`,
}}
closeButtonAriaLabel={closeButtonAriaLabel}
inputRef={inputRef}
inputValue={input}
inputLabel={inputFieldLabel}
errorMessage={errorMessage}
idkPostalCodeLinkProps={idkPostalCodeLinkProps}
onInput={(e) => {
errorMessage !== '' && setErrorMessage('')
setInput(e.currentTarget.value)
}}
onSubmit={handleSubmit}
fadeOutOnSubmit={true}
onClear={() => setInput('')}
/>
</Suspense>
<UIRegionModal
title={title}
description={description}
overlayProps={{
className: `section ${styles.section} section-region-modal`,
}}
closeButtonAriaLabel={closeButtonAriaLabel}
inputRef={inputRef}
inputValue={input}
inputLabel={inputFieldLabel}
errorMessage={errorMessage}
idkPostalCodeLinkProps={idkPostalCodeLinkProps}
onInput={(e) => {
errorMessage !== '' && setErrorMessage('')
setInput(e.currentTarget.value)
}}
onSubmit={handleSubmit}
fadeOutOnSubmit={true}
onClear={() => setInput('')}
/>
)}
</>
)
Expand Down

0 comments on commit 992cab3

Please sign in to comment.