-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
112 lines (100 loc) · 2.87 KB
/
index.js
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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import {
Button,
Popover,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { chevronDown } from '@wordpress/icons';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import LinkViewer from './link-viewer';
import LinkEditor from './link-editor';
import { unlock } from '../../lock-unlock';
const { __experimentalPopoverLegacyPositionToPlacement } = unlock(
componentsPrivateApis
);
const DEFAULT_PLACEMENT = 'bottom';
function URLPopover( {
additionalControls,
children,
renderSettings,
// The DEFAULT_PLACEMENT value is assigned inside the function's body
placement,
focusOnMount = 'firstElement',
// Deprecated
position,
// Rest
...popoverProps
} ) {
if ( position !== undefined ) {
deprecated( '`position` prop in wp.blockEditor.URLPopover', {
since: '6.2',
alternative: '`placement` prop',
} );
}
// Compute popover's placement:
// - give priority to `placement` prop, if defined
// - otherwise, compute it from the legacy `position` prop (if defined)
// - finally, fallback to the DEFAULT_PLACEMENT.
let computedPlacement;
if ( placement !== undefined ) {
computedPlacement = placement;
} else if ( position !== undefined ) {
computedPlacement =
__experimentalPopoverLegacyPositionToPlacement( position );
}
computedPlacement = computedPlacement || DEFAULT_PLACEMENT;
const [ isSettingsExpanded, setIsSettingsExpanded ] = useState( false );
const showSettings = !! renderSettings && isSettingsExpanded;
const toggleSettingsVisibility = () => {
setIsSettingsExpanded( ! isSettingsExpanded );
};
return (
<Popover
className="block-editor-url-popover"
focusOnMount={ focusOnMount }
placement={ computedPlacement }
shift
variant="toolbar"
{ ...popoverProps }
>
<div className="block-editor-url-popover__input-container">
<div className="block-editor-url-popover__row">
{ children }
{ !! renderSettings && (
<Button
className="block-editor-url-popover__settings-toggle"
icon={ chevronDown }
label={ __( 'Link settings' ) }
onClick={ toggleSettingsVisibility }
aria-expanded={ isSettingsExpanded }
size="compact"
/>
) }
</div>
{ showSettings && (
<div className="block-editor-url-popover__row block-editor-url-popover__settings">
{ renderSettings() }
</div>
) }
</div>
{ additionalControls && ! showSettings && (
<div className="block-editor-url-popover__additional-controls">
{ additionalControls }
</div>
) }
</Popover>
);
}
URLPopover.LinkEditor = LinkEditor;
URLPopover.LinkViewer = LinkViewer;
/**
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/url-popover/README.md
*/
export default URLPopover;