-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
TooltipLinkList.tsx
70 lines (61 loc) · 1.66 KB
/
TooltipLinkList.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
import type { SyntheticEvent } from 'react';
import React, { useCallback } from 'react';
import { styled } from '@storybook/theming';
import type { LinkWrapperType, ListItemProps } from './ListItem';
import ListItem from './ListItem';
const List = styled.div(
{
minWidth: 180,
overflow: 'hidden',
overflowY: 'auto',
maxHeight: 15.5 * 32, // 11.5 items
},
({ theme }) => ({
borderRadius: theme.appBorderRadius,
})
);
export interface Link extends Omit<ListItemProps, 'onClick'> {
id: string;
isGatsby?: boolean;
onClick?: (event: SyntheticEvent, item: ListItemProps) => void;
}
interface ItemProps extends Link {
isIndented?: boolean;
}
const Item = (props: ItemProps) => {
const { LinkWrapper, onClick: onClickFromProps, id, isIndented, ...rest } = props;
const { title, href, active } = rest;
const onClick = useCallback(
(event: SyntheticEvent) => {
onClickFromProps(event, rest);
},
[onClickFromProps]
);
const hasOnClick = !!onClickFromProps;
return (
<ListItem
title={title}
active={active}
href={href}
id={`list-item-${id}`}
LinkWrapper={LinkWrapper}
isIndented={isIndented}
{...rest}
{...(hasOnClick ? { onClick } : {})}
/>
);
};
export interface TooltipLinkListProps {
links: Link[];
LinkWrapper?: LinkWrapperType;
}
export const TooltipLinkList = ({ links, LinkWrapper = null }: TooltipLinkListProps) => {
const hasIcon = links.some((link) => link.icon);
return (
<List>
{links.map(({ isGatsby, ...p }) => (
<Item key={p.id} LinkWrapper={isGatsby ? LinkWrapper : null} isIndented={hasIcon} {...p} />
))}
</List>
);
};