-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathREADME.md
205 lines (145 loc) · 5.24 KB
/
README.md
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# URLPopover
URLPopover is a presentational React component used to render a popover used for editing and viewing a url.
## Setup
The component will be rendered adjacent to its parent.
```jsx
import { ToggleControl, Button } from '@wordpress/components';
import { URLPopover } from '@wordpress/block-editor';
import { keyboardReturn } from '@wordpress/icons';
class MyURLPopover extends Component {
constructor() {
super( ...arguments );
this.onChangeURL = this.onChangeURL.bind( this );
this.openURLPopover = this.openURLPopover.bind( this );
this.closeURLPopover = this.closeURLPopover.bind( this );
this.submitURL = this.submitURL.bind( this );
this.setTarget = this.setTarget.bind( this );
this.state = {
isVisible: false,
};
}
onChangeURL( url ) {
this.setState( { url } );
}
openURLPopover() {
this.setState( {
isVisible: true,
} );
}
closeURLPopover() {
this.setState( {
isVisible: false,
} );
}
submitURL() {
// Not shown: Store the updated url.
this.closeURLPopover();
}
setTarget() {
// Not shown: Store the updated 'opensInNewWindow' setting.
}
render() {
const { opensInNewWindow } = this.props;
const { url, isVisible, isEditing } = this.state;
return (
<>
<Button onClick={ this.openURLPopover }>Edit URL</Button>
{ isVisible && (
<URLPopover
onClose={ this.closeURLPopover }
renderSettings={ () => (
<ToggleControl
label={ __( 'Open in new tab' ) }
checked={ opensInNewWindow }
onChange={ this.setTarget }
/>
) }
>
<form onSubmit={ this.submitURL }>
<input
type="url"
value={ url }
onChange={ this.onChangeURL }
/>
<Button
icon={ keyboardReturn }
label={ __( 'Apply' ) }
type="submit"
/>
</form>
</URLPopover>
) }
</>
);
}
}
```
## Props
The component accepts the following props. Any other props are passed through to the underlying `Popover` component ([refer to props documentation](/packages/components/src/popover/README.md)).
### placement
Where the Popover should be positioned relative to its parent. Defaults to "bottom".
- Type: `String`
- Required: No
- Default: "bottom"
### focusOnMount
Provides control over which element is focused when the URLPopover mounts. Pass `false` to avoid focusing
an element.
- Type: `String`
- Required: No
- Default: "firstElement"
### renderSettings
Callback used to return the React Elements that will be rendered inside the settings drawer. When this function
is provided, a toggle button will be rendered in the popover that allows the user to open and close the settings
drawer.
- Type: `Function`
- Required: No
## Useful UI pieces
The URLPopover exposes two components that may be used as child components to make the UI creation process easier.
Although in the editor these components are used inside URLPopover and they were built with URLPopover use cases in mind, it maybe is possible and perfectly fine to use them standalone if they fit a use-case.
### LinkViewer
LinkViewer provides a simple UI that allows users to see a link and may also offer a button to switch to a mode that will enable editing that link.
The component accepts the following props. Any other props are passed through to the underlying `div` container.
### className
A class that together with "block-editor-url-popover**link-viewer" is used as a class of the wrapper div.
If no className is passed only "block-editor-url-popover**link-viewer" is used.
- Type: `String`
- Required: No
### linkClassName
A class that will be used in the component that renders the link.
- Type: `String`
- Required: No
### url
The current URL to view.
- Type: `String`
- Required: Yes
### urlLabel
The URL label, if not passed a label is automatically generated from the `url`.
- Type: `String`
- Required: No
### onEditLinkClick
A function called when the user presses the button that allows editing a link. If not passed the link-edit button is not rendered.
- Type: `function`
- Required: No
### LinkEditor
LinkEditor provides a simple UI that allows users to edit link.
The component accepts the following props. Any other props are passed through to the underlying `form` container.
### value
This property should be set to the attribute (or component state) property used to store the URL.
This property is directly passed to `URLInput` component ([refer to its documentation](/packages/components/src/url-input/README.md)) to read additional details.
- Type: `String`
- Required: Yes
### onChange
Called when the value changes. The second parameter is `null` unless the user selects a post from the suggestions dropdown.
More
This property is directly passed to component `URLInput` ([refer to its documentation](/packages/components/src/url-input/README.md)) to read additional details.
- Type: `function`
- Required: Yes
### autocompleteRef
Reference passed to the auto complete element of the ([URLInput component](/packages/components/src/url-input/README.md)).
- Type: `Object`
- Required: no
### position
_Note: this prop is deprecated. Please use the `placement` prop instead._
Where the Popover should be positioned relative to its parent.
- Type: `String`
- Required: No