Skip to content

Commit

Permalink
chore: Loader全体をmemo化する
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsushiM committed Jan 15, 2025
1 parent fd93c58 commit 63a4042
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions packages/smarthr-ui/src/components/Loader/Loader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ComponentProps, FC, ReactNode, useMemo } from 'react'
import React, { ComponentProps, ReactNode } from 'react'
import { tv } from 'tailwind-variants'

import { VisuallyHiddenText } from '../VisuallyHiddenText'
Expand Down Expand Up @@ -119,40 +119,46 @@ const loaderStyle = tv({
},
},
})
export const Loader: FC<Props & ElementProps> = ({
size = 'm',
alt = '処理中',
text,
type = 'primary',
role = 'status',
className,
...props
}) => {
const { wrapper, spinner, line, cog, cogInner, textSlot } = loaderStyle({
type,
size,
})
const wrapperStyle = useMemo(() => wrapper({ className }), [wrapper, className])
const spinnerStyle = useMemo(() => spinner(), [spinner])
const cogStyle = useMemo(() => cog(), [cog])
const textStyle = useMemo(() => textSlot(), [textSlot])

return (
<span {...props} className={wrapperStyle} role={role}>
<span className={spinnerStyle}>
{[...Array(4)].map((_, index) => (
<span className={line({ lineNum: (index + 1) as 1 | 2 | 3 | 4 })} key={index}>
<span className={cogStyle}>
<span className={cogInner({ position: 'left' })} />
</span>
<span className={cogStyle}>
<span className={cogInner({ position: 'right' })} />
export const Loader = React.memo<Props & ElementProps>(
({
size = 'm',
alt = '処理中',
text,
type = 'primary',
role = 'status',
className,
...props
}) => {
const { wrapper, spinner, line, cog, cogInner, textSlot } = loaderStyle({
type,
size,
})

const wrapperStyle = wrapper({ className })
const spinnerStyle = spinner()
const cogStyle = cog()
const cogInnerLeftStyle = cogInner({ position: 'left' })
const cogInnerRightStyle = cogInner({ position: 'right' })
const textStyle = textSlot()

return (
<span {...props} className={wrapperStyle} role={role}>
<span className={spinnerStyle}>
{[...Array(4)].map((_, index) => (
<span className={line({ lineNum: (index + 1) as 1 | 2 | 3 | 4 })} key={index}>
<span className={cogStyle}>
<span className={cogInnerLeftStyle} />
</span>
<span className={cogStyle}>
<span className={cogInnerRightStyle} />
</span>
</span>
</span>
))}
<VisuallyHiddenText>{alt}</VisuallyHiddenText>
))}
<VisuallyHiddenText>{alt}</VisuallyHiddenText>
</span>
{text && <span className={textStyle}>{text}</span>}
</span>
{text && <span className={textStyle}>{text}</span>}
</span>
)
}
)
},
)

0 comments on commit 63a4042

Please sign in to comment.