addons/isl/src/Link.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import * as stylex from '@stylexjs/stylex';
b69ab319import platform from './platform';
b69ab3110
b69ab3111const styles = stylex.create({
b69ab3112 a: {
b69ab3113 color: 'var(--link-foreground)',
b69ab3114 cursor: 'pointer',
b69ab3115 textDecoration: {
b69ab3116 ':hover': 'underline',
b69ab3117 },
b69ab3118 outline: {
b69ab3119 default: 'none',
b69ab3120 ':focus-visible': '1px solid var(--focus-border)',
b69ab3121 },
b69ab3122 },
b69ab3123});
b69ab3124
b69ab3125export function Link({
b69ab3126 children,
b69ab3127 href,
b69ab3128 onClick,
b69ab3129 xstyle,
b69ab3130 ...rest
b69ab3131}: React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement> & {
b69ab3132 xstyle?: stylex.StyleXStyles;
b69ab3133}) {
b69ab3134 const handleClick = (
b69ab3135 event: React.MouseEvent<HTMLAnchorElement> | React.KeyboardEvent<HTMLAnchorElement>,
b69ab3136 ) => {
b69ab3137 // allow pressing Enter when focused to simulate clicking for accessibility
b69ab3138 if (event.type === 'keyup') {
b69ab3139 if ((event as React.KeyboardEvent<HTMLAnchorElement>).key !== 'Enter') {
b69ab3140 return;
b69ab3141 }
b69ab3142 }
b69ab3143 if (href) {
b69ab3144 platform.openExternalLink(href);
b69ab3145 }
b69ab3146 onClick?.(event as React.MouseEvent<HTMLAnchorElement>);
b69ab3147 event.preventDefault();
b69ab3148 event.stopPropagation();
b69ab3149 };
b69ab3150 return (
b69ab3151 <a
b69ab3152 href={href}
b69ab3153 tabIndex={0}
b69ab3154 onKeyUp={handleClick}
b69ab3155 onClick={handleClick}
b69ab3156 {...stylex.props(styles.a, xstyle)}
b69ab3157 {...rest}>
b69ab3158 {children}
b69ab3159 </a>
b69ab3160 );
b69ab3161}