-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathicon-button.tsx
84 lines (78 loc) · 2.23 KB
/
icon-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React, { ComponentProps, FC, KeyboardEvent, RefObject } from 'react'
import classNames from 'classnames'
import { EbayIcon, Icon } from '../ebay-icon'
import { EbayBadge } from '../ebay-badge'
import { withForwardRef } from '../common/component-utils'
import { EbayKeyboardEventHandler } from '../common/event-utils/types'
import { Size } from '../ebay-button'
export type EbayIconButtonProps = {
href?: string;
icon: Icon;
badgeNumber?: number;
badgeAriaLabel?: string;
transparent?: boolean;
size?: Size;
forwardedRef?: RefObject<HTMLAnchorElement & HTMLButtonElement>;
onEscape?: EbayKeyboardEventHandler;
}
type HTMLButtonProps = ComponentProps<'button'>;
type HTMLAnchorProps = ComponentProps<'a'>;
type Props = EbayIconButtonProps & HTMLButtonProps & HTMLAnchorProps;
const EbayIconButton: FC<Props> = ({
href,
icon,
badgeNumber,
badgeAriaLabel,
transparent,
className: extraClasses,
forwardedRef,
size,
onEscape = () => {},
onKeyDown = () => {},
...rest
}: Props) => {
const classPrefix = href ? 'icon-link' : 'icon-btn'
const className = classNames(
extraClasses,
classPrefix,
size && `${classPrefix}--${size}`,
{
[`${classPrefix}--badged`]: badgeNumber,
[`${classPrefix}--transparent`]: transparent
}
)
const children = (
<>
<EbayIcon name={icon} />
{badgeNumber && <EbayBadge type="icon" number={badgeNumber} aria-label={badgeAriaLabel} />}
</>
)
const keyDownHandler = (e: KeyboardEvent<HTMLButtonElement & HTMLAnchorElement>) => {
if (e.key === 'Escape' || e.key === 'Esc') {
onEscape(e)
}
onKeyDown(e)
}
return href ? (
<a
ref={forwardedRef}
className={className}
href={href}
onKeyDown={keyDownHandler}
{...rest}
>
{children}
</a>
) : (
<button
ref={forwardedRef}
type="button"
className={className}
onKeyDown={keyDownHandler}
{...rest}
>
{children}
</button>
)
}
export default withForwardRef(EbayIconButton)