-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
92 lines (86 loc) · 2.65 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
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { displayShortcut } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { isPreviewingTheme } from '../../utils/is-previewing-theme';
export default function SaveButton( {
className = 'edit-site-save-button__button',
variant = 'primary',
showTooltip = true,
defaultLabel,
icon,
__next40pxDefaultSize = false,
} ) {
const { isDirty, isSaving, isSaveViewOpen } = useSelect( ( select ) => {
const { __experimentalGetDirtyEntityRecords, isSavingEntityRecord } =
select( coreStore );
const dirtyEntityRecords = __experimentalGetDirtyEntityRecords();
const { isSaveViewOpened } = select( editSiteStore );
return {
isDirty: dirtyEntityRecords.length > 0,
isSaving: dirtyEntityRecords.some( ( record ) =>
isSavingEntityRecord( record.kind, record.name, record.key )
),
isSaveViewOpen: isSaveViewOpened(),
};
}, [] );
const { setIsSaveViewOpened } = useDispatch( editSiteStore );
const activateSaveEnabled = isPreviewingTheme() || isDirty;
const disabled = isSaving || ! activateSaveEnabled;
const getLabel = () => {
if ( isPreviewingTheme() ) {
if ( isSaving ) {
return __( 'Activating' );
} else if ( disabled ) {
return __( 'Saved' );
} else if ( isDirty ) {
return __( 'Activate & Save' );
}
return __( 'Activate' );
}
if ( isSaving ) {
return __( 'Saving' );
} else if ( disabled ) {
return __( 'Saved' );
} else if ( defaultLabel ) {
return defaultLabel;
}
return __( 'Save' );
};
const label = getLabel();
return (
<Button
variant={ variant }
className={ className }
aria-disabled={ disabled }
aria-expanded={ isSaveViewOpen }
isBusy={ isSaving }
onClick={ disabled ? undefined : () => setIsSaveViewOpened( true ) }
label={ label }
/*
* We want the tooltip to show the keyboard shortcut only when the
* button does something, i.e. when it's not disabled.
*/
shortcut={ disabled ? undefined : displayShortcut.primary( 's' ) }
/*
* Displaying the keyboard shortcut conditionally makes the tooltip
* itself show conditionally. This would trigger a full-rerendering
* of the button that we want to avoid. By setting `showTooltip`,
& the tooltip is always rendered even when there's no keyboard shortcut.
*/
showTooltip={ showTooltip }
icon={ icon }
__next40pxDefaultSize={ __next40pxDefaultSize }
>
{ label }
</Button>
);
}