-
Notifications
You must be signed in to change notification settings - Fork 518
Expand file tree
/
Copy pathraised-pill.tsx
More file actions
94 lines (87 loc) · 1.98 KB
/
raised-pill.tsx
File metadata and controls
94 lines (87 loc) · 1.98 KB
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
85
86
87
88
89
90
91
92
93
94
import React from 'react'
import stringWidth from 'string-width'
import { Button } from './button'
type PillSegment = {
text: string
fg?: string
attr?: number
}
interface RaisedPillProps {
segments: PillSegment[]
frameColor: string
textColor: string
fillColor?: string
padding?: number
onPress?: () => void
style?: Record<string, unknown>
}
const buildHorizontal = (length: number): string => {
if (length <= 0) return ''
return '─'.repeat(length)
}
export const RaisedPill = ({
segments,
frameColor,
textColor,
fillColor,
padding = 2,
onPress,
style,
}: RaisedPillProps): React.ReactNode => {
const leftRightPadding =
padding > 0
? [{ text: ' '.repeat(padding), fg: textColor }]
: []
const normalizedSegments: Array<{
text: string
fg?: string
attr?: number
}> = [
...leftRightPadding,
...segments.map((segment) => ({
text: segment.text,
fg: segment.fg ?? textColor,
attr: segment.attr,
})),
...leftRightPadding,
]
const contentText = normalizedSegments.map((segment) => segment.text).join('')
const contentWidth = Math.max(0, stringWidth(contentText))
const horizontal = buildHorizontal(contentWidth)
return (
<Button
style={{
flexDirection: 'column',
gap: 0,
backgroundColor: 'transparent',
...style,
}}
onClick={onPress}
>
<text>
<span fg={frameColor}>{`╭${horizontal}╮`}</span>
</text>
<text>
<span fg={frameColor}>
│
</span>
{normalizedSegments.map((segment, idx) => (
<span
key={idx}
fg={segment.fg}
bg={fillColor ?? 'transparent'}
attributes={segment.attr}
>
{segment.text}
</span>
))}
<span fg={frameColor}>
│
</span>
</text>
<text>
<span fg={frameColor}>{`╰${horizontal}╯`}</span>
</text>
</Button>
)
}