@@ -17,9 +17,11 @@ import {
17
17
} from '@techstack/react-feather' ;
18
18
import { $findMatchingParent , mergeRegister } from '@lexical/utils' ;
19
19
import { $isLinkNode } from '@lexical/link' ;
20
+ import { $isParentElementRTL } from '@lexical/selection' ;
20
21
21
22
import { LowPriority } from '../utils/priorities' ;
22
23
import getSelectedNode from '../utils/getSelectedNode' ;
24
+ import DropDown , { DropDownItem } from '../ui/Dropdown' ;
23
25
24
26
const ELEMENT_FORMAT_OPTIONS : {
25
27
[ key in Exclude < ElementFormatType , '' > ] : {
@@ -62,9 +64,8 @@ const ELEMENT_FORMAT_OPTIONS: {
62
64
63
65
const ListPlugin = ( ) => {
64
66
const [ editor ] = useLexicalComposerContext ( ) ;
65
- const dropDownRef = useRef < HTMLDivElement > ( null ) ;
67
+ const [ isRTL , setIsRTL ] = useState ( false ) ;
66
68
67
- const [ showListOptionsDropdown , setShowListOptionsDropdown ] = useState ( false ) ;
68
69
const [ elementFormat , setElementFormat ] = useState < ElementFormatType > ( 'left' ) ;
69
70
70
71
const formatOption = ELEMENT_FORMAT_OPTIONS [ elementFormat || 'left' ] ;
@@ -84,6 +85,7 @@ const ListPlugin = () => {
84
85
) ;
85
86
}
86
87
88
+ setIsRTL ( $isParentElementRTL ( selection ) ) ;
87
89
setElementFormat (
88
90
$isElementNode ( matchingParent )
89
91
? matchingParent . getFormatType ( )
@@ -115,72 +117,84 @@ const ListPlugin = () => {
115
117
) ;
116
118
117
119
return (
118
- < >
119
- < button
120
- className = 'toolbar-item block-controls'
121
- type = 'button'
122
- onClick = { ( ) => setShowListOptionsDropdown ( prevState => ! prevState ) }
123
- aria-label = 'Formatting Options'
120
+ < DropDown buttonIcon = { formatOption . icon } buttonLabel = { formatOption . name } >
121
+ < DropDownItem
122
+ onClick = { ( ) => {
123
+ editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'left' ) ;
124
+ } }
124
125
>
125
- < span className = { `icon` } > { formatOption . icon } </ span >
126
- < span className = 'text' > { formatOption . name } </ span >
127
- < i className = 'chevron-down' >
128
- < ChevronDown />
126
+ < i className = 'icon' >
127
+ < AlignLeft />
129
128
</ i >
130
- </ button >
131
- { showListOptionsDropdown && (
132
- < div className = 'toolbar-dropdown' ref = { dropDownRef } >
133
- < button
134
- type = 'button'
135
- onClick = { ( ) => {
136
- editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'left' ) ;
137
- } }
138
- className = 'toolbar-item spaced'
139
- aria-label = 'Left Align'
140
- >
141
- < i className = 'format left-align' >
142
- < AlignLeft size = { 14 } />
143
- </ i >
144
- </ button >
145
- < button
146
- type = 'button'
147
- onClick = { ( ) => {
148
- editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'center' ) ;
149
- } }
150
- className = 'toolbar-item spaced'
151
- aria-label = 'Center Align'
152
- >
153
- < i className = 'format center-align' >
154
- < AlignCenter size = { 14 } />
155
- </ i >
156
- </ button >
157
- < button
158
- type = 'button'
159
- onClick = { ( ) => {
160
- editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'right' ) ;
161
- } }
162
- className = 'toolbar-item spaced'
163
- aria-label = 'Right Align'
164
- >
165
- < i className = 'format right-align' >
166
- < AlignRight size = { 14 } />
167
- </ i >
168
- </ button >
169
- < button
170
- type = 'button'
171
- onClick = { ( ) => {
172
- editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'justify' ) ;
173
- } }
174
- className = 'toolbar-item'
175
- aria-label = 'Justify Align'
176
- >
177
- < i className = 'format justify-align' >
178
- < AlignJustify size = { 14 } />
179
- </ i >
180
- </ button >
181
- </ div >
182
- ) }
183
- </ >
129
+ < span className = "text" >
130
+ Align Left
131
+ </ span >
132
+ </ DropDownItem >
133
+ < DropDownItem
134
+ onClick = { ( ) => {
135
+ editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'center' ) ;
136
+ } }
137
+ >
138
+ < i className = 'icon' >
139
+ < AlignCenter />
140
+ </ i >
141
+ < span className = "text" >
142
+ Align Center
143
+ </ span >
144
+ </ DropDownItem >
145
+ < DropDownItem
146
+ onClick = { ( ) => {
147
+ editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'right' ) ;
148
+ } }
149
+ >
150
+ < i className = 'icon' >
151
+ < AlignRight />
152
+ </ i >
153
+ < span className = "text" >
154
+ Align Right
155
+ </ span >
156
+ </ DropDownItem >
157
+ < DropDownItem
158
+ onClick = { ( ) => {
159
+ editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'justify' ) ;
160
+ } }
161
+ >
162
+ < i className = 'icon' >
163
+ < AlignJustify />
164
+ </ i >
165
+ < span className = "text" >
166
+ Align Justify
167
+ </ span >
168
+ </ DropDownItem >
169
+ < DropDownItem
170
+ onClick = { ( ) => {
171
+ editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'start' ) ;
172
+ } }
173
+ >
174
+ < i className = 'icon' >
175
+ { isRTL
176
+ ? ELEMENT_FORMAT_OPTIONS . start . iconRTL
177
+ : ELEMENT_FORMAT_OPTIONS . start . icon }
178
+ </ i >
179
+ < span className = "text" >
180
+ Align Start
181
+ </ span >
182
+ </ DropDownItem >
183
+ < DropDownItem
184
+ onClick = { ( ) => {
185
+ editor . dispatchCommand ( FORMAT_ELEMENT_COMMAND , 'end' ) ;
186
+ } }
187
+ >
188
+ < i className = 'icon' >
189
+ { isRTL
190
+ ? ELEMENT_FORMAT_OPTIONS . end . iconRTL
191
+ : ELEMENT_FORMAT_OPTIONS . end . icon }
192
+ </ i >
193
+ < span className = "text" >
194
+ Align End
195
+ </ span >
196
+ </ DropDownItem >
197
+ </ DropDown >
184
198
) ;
185
199
} ;
186
200
export default ListPlugin ;
0 commit comments