-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit component.js files to presentation
- Loading branch information
1 parent
ed518fd
commit f9411d1
Showing
6 changed files
with
173 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 4 additions & 65 deletions
69
packages/components/src/tools-panel/tools-panel-item/component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 65 additions & 6 deletions
71
packages/components/src/tools-panel/tools-panel-item/hook.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,88 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { usePrevious } from '@wordpress/compose'; | ||
import { useEffect, useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as styles from '../styles'; | ||
import { useToolsPanelContext } from '../context'; | ||
import { MENU_STATES } from '../utils'; | ||
import { useContextSystem } from '../../ui/context'; | ||
import { useCx } from '../../utils/hooks/use-cx'; | ||
|
||
export function useToolsPanelItem( props ) { | ||
const { className, ...otherProps } = useContextSystem( | ||
props, | ||
'ToolsPanel' | ||
); | ||
const { | ||
className, | ||
hasValue, | ||
isShownByDefault, | ||
label, | ||
onDeselect = () => undefined, | ||
onSelect = () => undefined, | ||
...otherProps | ||
} = useContextSystem( props, 'ToolsPanelItem' ); | ||
|
||
const cx = useCx(); | ||
|
||
const classes = useMemo( () => { | ||
return cx( styles.ToolsPanelItem, className ); | ||
} ); | ||
|
||
const { | ||
checkMenuItem, | ||
menuItems, | ||
registerPanelItem, | ||
} = useToolsPanelContext(); | ||
|
||
// Registering the panel item allows the panel to include it in its | ||
// automatically generated menu and determine its initial checked status. | ||
useEffect( () => { | ||
registerPanelItem( { | ||
hasValue, | ||
isShownByDefault, | ||
label, | ||
} ); | ||
}, [] ); | ||
|
||
const isValueSet = hasValue(); | ||
|
||
// When the user sets a value on the panel item's control, tell the panel | ||
// to check its corresponding menu item. | ||
useEffect( () => { | ||
if ( isValueSet ) { | ||
checkMenuItem( label ); | ||
} | ||
}, [ isValueSet ] ); | ||
|
||
// Note: `label` is used as a key when building menu item state in | ||
// `ToolsPanel`. | ||
const isShown = menuItems[ label ] !== MENU_STATES.UNCHECKED; | ||
const isMenuItemChecked = menuItems[ label ] === MENU_STATES.CHECKED; | ||
const wasMenuItemChecked = usePrevious( isMenuItemChecked ); | ||
|
||
// Determine if the panel item's corresponding menu it is being toggled and | ||
// trigger appropriate callback if it is. | ||
useEffect( () => { | ||
// If the panel's menu item is now checked but wasn't previously and | ||
// we don't have a current value, consider the menu item as having just | ||
// been selected. | ||
if ( | ||
isMenuItemChecked && | ||
! isValueSet && | ||
wasMenuItemChecked === false | ||
) { | ||
onSelect(); | ||
} | ||
|
||
if ( ! isMenuItemChecked && wasMenuItemChecked ) { | ||
onDeselect(); | ||
} | ||
}, [ isMenuItemChecked, wasMenuItemChecked, isValueSet ] ); | ||
|
||
return { | ||
...otherProps, | ||
isShown, | ||
className: classes, | ||
}; | ||
} |
98 changes: 5 additions & 93 deletions
98
packages/components/src/tools-panel/tools-panel/component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.