| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import * as stylex from '@stylexjs/stylex'; |
| 9 | import platform from './platform'; |
| 10 | |
| 11 | const styles = stylex.create({ |
| 12 | a: { |
| 13 | color: 'var(--link-foreground)', |
| 14 | cursor: 'pointer', |
| 15 | textDecoration: { |
| 16 | ':hover': 'underline', |
| 17 | }, |
| 18 | outline: { |
| 19 | default: 'none', |
| 20 | ':focus-visible': '1px solid var(--focus-border)', |
| 21 | }, |
| 22 | }, |
| 23 | }); |
| 24 | |
| 25 | export function Link({ |
| 26 | children, |
| 27 | href, |
| 28 | onClick, |
| 29 | xstyle, |
| 30 | ...rest |
| 31 | }: React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement> & { |
| 32 | xstyle?: stylex.StyleXStyles; |
| 33 | }) { |
| 34 | const handleClick = ( |
| 35 | event: React.MouseEvent<HTMLAnchorElement> | React.KeyboardEvent<HTMLAnchorElement>, |
| 36 | ) => { |
| 37 | // allow pressing Enter when focused to simulate clicking for accessibility |
| 38 | if (event.type === 'keyup') { |
| 39 | if ((event as React.KeyboardEvent<HTMLAnchorElement>).key !== 'Enter') { |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | if (href) { |
| 44 | platform.openExternalLink(href); |
| 45 | } |
| 46 | onClick?.(event as React.MouseEvent<HTMLAnchorElement>); |
| 47 | event.preventDefault(); |
| 48 | event.stopPropagation(); |
| 49 | }; |
| 50 | return ( |
| 51 | <a |
| 52 | href={href} |
| 53 | tabIndex={0} |
| 54 | onKeyUp={handleClick} |
| 55 | onClick={handleClick} |
| 56 | {...stylex.props(styles.a, xstyle)} |
| 57 | {...rest}> |
| 58 | {children} |
| 59 | </a> |
| 60 | ); |
| 61 | } |
| 62 | |