Skip to content

Commit 46e4200

Browse files
authoredJun 14, 2024··
feat: anchor系コンポーネントがtarget属性を持つ場合、rel属性を自動設定する (#4720)
1 parent 71053ed commit 46e4200

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed
 

‎packages/smarthr-ui/src/components/Button/AnchorButton.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ export const AnchorButton = forwardRef<HTMLAnchorElement, BaseProps & ElementPro
2020
suffix,
2121
wide = false,
2222
variant = 'secondary',
23+
target,
24+
rel,
2325
className,
2426
children,
2527
...props
2628
},
2729
ref,
2830
) => {
2931
const styles = useMemo(() => anchorButton({ className }), [className])
32+
const actualRel = useMemo(
33+
() => (rel === undefined && target === '_blank' ? 'noopener noreferrer' : rel),
34+
[rel, target],
35+
)
3036

3137
return (
3238
<ButtonWrapper
@@ -36,6 +42,8 @@ export const AnchorButton = forwardRef<HTMLAnchorElement, BaseProps & ElementPro
3642
wide={wide}
3743
variant={variant}
3844
className={styles}
45+
target={target}
46+
rel={actualRel}
3947
isAnchor
4048
anchorRef={ref}
4149
>

‎packages/smarthr-ui/src/components/TextLink/TextLink.stories.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export const All: StoryFn = () => (
2626
別タブで開くルートへのリンク
2727
</TextLink>
2828
</li>
29+
<li>
30+
<TextLink href="/" target="_blank" rel={null}>
31+
別タブで開くルートへのリンク(rel属性 なし)
32+
</TextLink>
33+
</li>
2934
<li>
3035
<TextLink href="/" target="_blank" suffix={null}>
3136
別タブで開くルートへのリンク(suffix なし)

‎packages/smarthr-ui/src/components/TextLink/TextLink.tsx

+14-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ const textLink = tv({
2121
suffixWrapper: 'shr-ms-0.25 shr-align-middle',
2222
},
2323
})
24+
const { anchor, prefixWrapper, suffixWrapper } = textLink()
25+
const prefixWrapperClassName = prefixWrapper()
26+
const suffixWrapperClassName = suffixWrapper()
2427

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

4245
return undefined
4346
}, [href, onClick])
47+
const actualRel = useMemo(
48+
() => (rel === undefined && target === '_blank' ? 'noopener noreferrer' : rel),
49+
[rel, target],
50+
)
51+
const anchorClassName = useMemo(() => anchor({ className }), [className])
52+
4453
const actualOnClick = useMemo(() => {
4554
if (!onClick) {
4655
return undefined
@@ -54,20 +63,19 @@ export const TextLink = forwardRef<HTMLAnchorElement, Props & ElementProps>(
5463
}
5564
}, [href, onClick])
5665

57-
const { anchor, prefixWrapper, suffixWrapper } = useMemo(() => textLink(), [])
58-
5966
return (
6067
<a
6168
{...others}
6269
ref={ref}
6370
href={actualHref}
6471
target={target}
72+
rel={actualRel}
6573
onClick={actualOnClick}
66-
className={anchor({ className })}
74+
className={anchorClassName}
6775
>
68-
{prefix && <span className={prefixWrapper()}>{prefix}</span>}
76+
{prefix && <span className={prefixWrapperClassName}>{prefix}</span>}
6977
{children}
70-
{actualSuffix && <span className={suffixWrapper()}>{actualSuffix}</span>}
78+
{actualSuffix && <span className={suffixWrapperClassName}>{actualSuffix}</span>}
7179
</a>
7280
)
7381
},

0 commit comments

Comments
 (0)
Please sign in to comment.