-
Notifications
You must be signed in to change notification settings - Fork 141
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
chore: テキスト系のシンプルなコンポーネントをmemo化する #5406
base: master
Are you sure you want to change the base?
Changes from all commits
71c2333
659a43a
05ebfde
48567d7
c262355
137872d
f5e52e8
8ef3027
dfd527e
881310d
300935f
611e4ce
ff3f640
2b94b5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import React, { type ComponentProps, useMemo } from 'react' | ||
import React, { type ComponentProps, memo, useMemo } from 'react' | ||
import { VariantProps, tv } from 'tailwind-variants' | ||
|
||
import { FaArrowLeftIcon } from '../Icon' | ||
import { TextLink } from '../TextLink' | ||
|
||
const styleGenerator = tv({ | ||
const classNameGenerator = tv({ | ||
base: 'shr-leading-none', | ||
variants: { | ||
indent: { | ||
|
@@ -15,17 +15,20 @@ const styleGenerator = tv({ | |
}) | ||
|
||
type Props = Omit<ComponentProps<typeof TextLink>, 'prefix' | 'suffix'> & | ||
VariantProps<typeof styleGenerator> & { | ||
VariantProps<typeof classNameGenerator> & { | ||
/** `TextLink`に渡す `elementAs` をオプションで指定 */ | ||
elementAs?: ComponentProps<typeof TextLink>['elementAs'] | ||
} | ||
|
||
export const UpwardLink: React.FC<Props> = ({ indent = true, className, elementAs, ...rest }) => { | ||
const style = useMemo(() => styleGenerator({ indent, className }), [indent, className]) | ||
export const UpwardLink = memo<Props>(({ indent, className, ...rest }) => { | ||
const actualClassName = useMemo( | ||
() => classNameGenerator({ indent: indent ?? true, className }), | ||
[indent, className], | ||
) | ||
|
||
return ( | ||
<div className={style}> | ||
<TextLink {...rest} elementAs={elementAs} prefix={<FaArrowLeftIcon />} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. elementAsが消えているように見えますが、https://github.com/kufu/smarthr-ui/pull/5406/files#diff-5f096a050e1946adb3b1a45c9737159ebceea44f39788f072ea5b19eee4385baR23 でrestに含めるようにしているため、指定としては同じ状態になっています |
||
<div className={actualClassName}> | ||
<TextLink {...rest} prefix={<FaArrowLeftIcon />} /> | ||
</div> | ||
) | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import React, { ComponentProps, PropsWithChildren, useMemo } from 'react' | ||
import React, { type ComponentProps, type PropsWithChildren, memo, useMemo } from 'react' | ||
import { tv } from 'tailwind-variants' | ||
|
||
export const visuallyHiddenText = tv({ | ||
export const visuallyHiddenTextClassNameGenerator = tv({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
base: 'shr-absolute -shr-top-px shr-left-0 shr-h-px shr-w-px shr-overflow-hidden shr-whitespace-nowrap shr-border-0 shr-p-0 [clip-path:inset(100%)] [clip:rect(0_0_0_0)]', | ||
}) | ||
|
||
|
@@ -10,12 +10,17 @@ type Props<T extends React.ElementType> = PropsWithChildren<{ | |
}> & | ||
ComponentProps<T> | ||
|
||
export const VisuallyHiddenText = <T extends React.ElementType = 'span'>({ | ||
const ActualVisuallyHiddenText = <T extends React.ElementType = 'span'>({ | ||
as: Component = 'span', | ||
className, | ||
...props | ||
}: Props<T>) => { | ||
const styles = useMemo(() => visuallyHiddenText({ className }), [className]) | ||
const actualClassName = useMemo( | ||
() => visuallyHiddenTextClassNameGenerator({ className }), | ||
[className], | ||
) | ||
|
||
return <Component {...props} className={styles} /> | ||
return <Component {...props} className={actualClassName} /> | ||
} | ||
|
||
export const VisuallyHiddenText = memo(ActualVisuallyHiddenText) as typeof ActualVisuallyHiddenText |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { VisuallyHiddenText, visuallyHiddenText } from './VisuallyHiddenText' | ||
export { VisuallyHiddenText, visuallyHiddenTextClassNameGenerator } from './VisuallyHiddenText' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
genericsを使ったコンポーネントをmemo化する場合、asで明確に型付けし直すことで問題なく設定できます。
https://www.google.com/search?q=typescript+generics+memo&oq=typescript+generics+memo&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIHCAEQABjvBTIHCAIQABjvBTIHCAMQABjvBTIHCAQQABjvBTIHCAUQABjvBdIBCDkyMjhqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8