Skip to content

Commit

Permalink
Merge branch 'master' into SHRUI-989
Browse files Browse the repository at this point in the history
  • Loading branch information
s-sasaki-0529 authored Jun 14, 2024
2 parents 3d12c96 + 46e4200 commit 09233d2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
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

0 comments on commit 09233d2

Please sign in to comment.