Skip to content
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

feat: anchor系コンポーネントがtarget属性を持つ場合、rel属性を自動設定する #4720

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/smarthr-ui/src/components/Button/AnchorButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ export const AnchorButton = forwardRef<HTMLAnchorElement, BaseProps & ElementPro
suffix,
wide = false,
variant = 'secondary',
target,
rel,
className,
children,
...props
},
ref,
) => {
const styles = useMemo(() => anchorButton({ className }), [className])
const actualRel = useMemo(
() => (rel === undefined && target === '_blank' ? 'noopener noreferrer' : rel),
[rel, target],
)

return (
<ButtonWrapper
Expand All @@ -36,6 +42,8 @@ export const AnchorButton = forwardRef<HTMLAnchorElement, BaseProps & ElementPro
wide={wide}
variant={variant}
className={styles}
target={target}
rel={actualRel}
isAnchor
anchorRef={ref}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export const All: StoryFn = () => (
別タブで開くルートへのリンク
</TextLink>
</li>
<li>
<TextLink href="/" target="_blank" rel={null}>
別タブで開くルートへのリンク(rel属性 なし)
</TextLink>
</li>
<li>
<TextLink href="/" target="_blank" suffix={null}>
別タブで開くルートへのリンク(suffix なし)
Expand Down
20 changes: 14 additions & 6 deletions packages/smarthr-ui/src/components/TextLink/TextLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ const textLink = tv({
suffixWrapper: 'shr-ms-0.25 shr-align-middle',
},
})
const { anchor, prefixWrapper, suffixWrapper } = textLink()
const prefixWrapperClassName = prefixWrapper()
const suffixWrapperClassName = suffixWrapper()

export const TextLink = forwardRef<HTMLAnchorElement, Props & ElementProps>(
({ href, target, onClick, children, prefix, suffix, className, ...others }, ref) => {
({ href, target, rel, onClick, children, prefix, suffix, className, ...others }, ref) => {
const actualSuffix = useMemo(() => {
if (target === '_blank' && suffix === undefined) {
return <FaExternalLinkAltIcon aria-label="別タブで開く" />
Expand All @@ -41,6 +44,12 @@ export const TextLink = forwardRef<HTMLAnchorElement, Props & ElementProps>(

return undefined
}, [href, onClick])
const actualRel = useMemo(
() => (rel === undefined && target === '_blank' ? 'noopener noreferrer' : rel),
[rel, target],
)
const anchorClassName = useMemo(() => anchor({ className }), [className])

const actualOnClick = useMemo(() => {
if (!onClick) {
return undefined
Expand All @@ -54,20 +63,19 @@ export const TextLink = forwardRef<HTMLAnchorElement, Props & ElementProps>(
}
}, [href, onClick])

const { anchor, prefixWrapper, suffixWrapper } = useMemo(() => textLink(), [])

return (
<a
{...others}
ref={ref}
href={actualHref}
target={target}
rel={actualRel}
onClick={actualOnClick}
className={anchor({ className })}
className={anchorClassName}
>
{prefix && <span className={prefixWrapper()}>{prefix}</span>}
{prefix && <span className={prefixWrapperClassName}>{prefix}</span>}
{children}
{actualSuffix && <span className={suffixWrapper()}>{actualSuffix}</span>}
{actualSuffix && <span className={suffixWrapperClassName}>{actualSuffix}</span>}
</a>
)
},
Expand Down