/
BlockStyleSelect.tsx
172 lines (147 loc) · 4.73 KB
/
BlockStyleSelect.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, {memo, useCallback, useEffect, useMemo, useState} from 'react'
import {PortableTextEditor, usePortableTextEditor} from '@sanity/portable-text-editor'
import {Button, Menu, MenuButton, MenuButtonProps, MenuItem, Text} from '@sanity/ui'
import {SelectIcon} from '@sanity/icons'
import styled from 'styled-components'
import {
Heading1,
Heading2,
Heading3,
Heading4,
Heading5,
Heading6,
BlockQuote,
Normal,
} from '../text/textStyles'
import {useActiveStyleKeys, useFocusBlock} from './hooks'
import {BlockStyleItem} from './types'
const MenuButtonMemo = memo(MenuButton)
interface BlockStyleSelectProps {
disabled: boolean
items: BlockStyleItem[]
}
const StyledMenuItem = styled(MenuItem)`
// Change the border color variable used by BlockQuote
// to make the border visible when the MenuItem is selected
&[data-selected] {
[data-option='blockquote'] {
--card-border-color: var(--card-muted-fg-color);
}
}
`
const StyledButton = styled(Button)`
width: 100%;
`
const MENU_POPOVER_PROPS: MenuButtonProps['popover'] = {
constrainSize: true,
placement: 'bottom-start',
portal: 'default',
}
const TEXT_STYLE_OPTIONS: Record<string, (title: React.ReactNode) => React.ReactNode> = {
h1: (title) => <Heading1>{title}</Heading1>,
h2: (title) => <Heading2>{title}</Heading2>,
h3: (title) => <Heading3>{title}</Heading3>,
h4: (title) => <Heading4>{title}</Heading4>,
h5: (title) => <Heading5>{title}</Heading5>,
h6: (title) => <Heading6>{title}</Heading6>,
normal: (title) => <Normal>{title}</Normal>,
blockquote: (title) => <BlockQuote data-option="blockquote">{title}</BlockQuote>,
}
const TEXT_STYLE_KEYS = Object.keys(TEXT_STYLE_OPTIONS)
const preventDefault = (event: React.MouseEvent<HTMLButtonElement>) => event.preventDefault()
const emptyStyle: BlockStyleItem = {
key: 'style-none',
style: '',
title: 'No style',
}
export const BlockStyleSelect = memo(function BlockStyleSelect(
props: BlockStyleSelectProps
): JSX.Element {
const {disabled, items: itemsProp} = props
const editor = usePortableTextEditor()
const focusBlock = useFocusBlock()
const [changed, setChanged] = useState(false)
const _disabled =
disabled || (focusBlock ? editor.schemaTypes.block.name !== focusBlock._type : false)
// @todo: Explain what this does
const activeKeys = useActiveStyleKeys({items: itemsProp})
const {activeItems, items} = useMemo(() => {
const _activeItems = itemsProp.filter((item) => activeKeys.includes(item.style))
let _items = itemsProp
if (_activeItems.length === 0 && _items.length > 1) {
_items = _items.concat([emptyStyle])
_activeItems.push(emptyStyle)
}
return {activeItems: _activeItems, items: _items}
}, [activeKeys, itemsProp])
const menuButtonText = useMemo(() => {
if (activeItems.length > 1) return 'Multiple'
if (activeItems.length === 1) return activeItems[0].title
return emptyStyle.title
}, [activeItems])
const handleChange = useCallback(
(item: BlockStyleItem): void => {
if (focusBlock && item.style !== focusBlock.style) {
PortableTextEditor.toggleBlockStyle(editor, item.style)
}
setChanged(true)
},
[editor, focusBlock]
)
const renderOption = useCallback((style: string, title: string) => {
const hasTextStyle = TEXT_STYLE_KEYS.includes(style)
const renderStyle = TEXT_STYLE_OPTIONS[style]
if (hasTextStyle) {
return renderStyle(title)
}
return <Text>{title}</Text>
}, [])
const button = useMemo(
() => (
<StyledButton
disabled={_disabled}
iconRight={SelectIcon}
mode="bleed"
onClick={preventDefault}
padding={2}
text={menuButtonText}
/>
),
[_disabled, menuButtonText]
)
const menu = useMemo(
() => (
<Menu disabled={_disabled}>
{items.map((item) => {
return (
<StyledMenuItem
key={item.key}
pressed={activeItems.includes(item)}
// eslint-disable-next-line react/jsx-no-bind
onClick={_disabled ? undefined : () => handleChange(item)}
>
{renderOption(item.style, item?.title || item.style)}
</StyledMenuItem>
)
})}
</Menu>
),
[_disabled, activeItems, handleChange, items, renderOption]
)
// Set focus back into the editor when the new value get's in
// This must be the last registered hook or it will not be able to focus properly.
useEffect(() => {
if (changed) {
PortableTextEditor.focus(editor)
setChanged(false)
}
}, [activeItems, changed, editor])
return (
<MenuButtonMemo
popover={MENU_POPOVER_PROPS}
id="block-style-select"
button={button}
menu={menu}
/>
)
})