diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index facbc86f3122e2..7ea421e53d9084 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -11,6 +11,7 @@ - `Tooltip (Experimental)`, `CustomSelectControl`, `TimePicker`: Add missing font-size styles which were necessary in non-WordPress contexts ([#42844](https://github.com/WordPress/gutenberg/pull/42844/)). - `TextControl`, `TextareaControl`, `ToggleGroupControl`: Add `box-sizing` reset style ([#42889](https://github.com/WordPress/gutenberg/pull/42889)). - `Popover`: fix arrow placement and design ([#42874](https://github.com/WordPress/gutenberg/pull/42874/)). +- `Popover`: fix minor glitch in arrow [#42903](https://github.com/WordPress/gutenberg/pull/42903)). ### Enhancements @@ -23,6 +24,7 @@ - `Flex`, `FlexItem`, `FlexBlock`: Convert to TypeScript ([#42537](https://github.com/WordPress/gutenberg/pull/42537)). - `InputControl`: Fix incorrect `size` prop passing ([#42793](https://github.com/WordPress/gutenberg/pull/42793)). +- `Popover`: rewrite Storybook examples using controls [#42903](https://github.com/WordPress/gutenberg/pull/42903)). ## 19.16.0 (2022-07-27) diff --git a/packages/components/src/popover/stories/_utils.js b/packages/components/src/popover/stories/_utils.js deleted file mode 100644 index c99c23190e3603..00000000000000 --- a/packages/components/src/popover/stories/_utils.js +++ /dev/null @@ -1,64 +0,0 @@ -/** - * WordPress dependencies - */ -import { useCallback, useEffect, useState } from '@wordpress/element'; - -export const DraggableWrapper = ( - props = { role: 'presentation', style: {} } -) => { - const { role, style, ...restProps } = props; - const [ position, setPosition ] = useState( { x: 0, y: 0 } ); - const [ isDragging, setDragging ] = useState( false ); - - const updatePosition = useCallback( - ( event ) => { - if ( ! isDragging ) { - return false; - } - const { movementX, movementY } = event; - setPosition( { - ...position, - x: position.x + movementX, - y: position.y + movementY, - } ); - }, - [ isDragging, position ] - ); - - const startDragging = useCallback( () => setDragging( true ), [] ); - const stopDragging = useCallback( () => setDragging( false ), [] ); - - useEffect( () => { - document.addEventListener( 'mousemove', updatePosition ); - document.addEventListener( 'mouseup', stopDragging ); - return () => { - document.removeEventListener( 'mousemove', updatePosition ); - document.removeEventListener( 'mouseup', stopDragging ); - }; - }, [ updatePosition, stopDragging ] ); - - const { x, y } = position; - - /** - * Ideally, the x and y coordinates would be applied transform: translate. - * However, using transform is causing layout rendering issues when - * combined with components like Popover (due to their positioning - * strategy). - */ - const componentStyle = { - ...style, - position: 'relative', - top: y, - left: x, - }; - - return ( -
- ); -}; diff --git a/packages/components/src/popover/stories/index.js b/packages/components/src/popover/stories/index.js index 0025e66fe0f4e1..32bcb6463d677e 100644 --- a/packages/components/src/popover/stories/index.js +++ b/packages/components/src/popover/stories/index.js @@ -1,123 +1,176 @@ -/** - * External dependencies - */ -import { boolean, select, text } from '@storybook/addon-knobs'; - /** * WordPress dependencies */ -import { useState } from '@wordpress/element'; +import { useState, useRef } from '@wordpress/element'; +import { __unstableIframe as Iframe } from '@wordpress/block-editor'; /** * Internal dependencies */ -import { DraggableWrapper } from './_utils'; -import Popover from '../'; import Button from '../../button'; +import { Provider as SlotFillProvider } from '../../slot-fill'; +import Popover from '../'; + +// Format: "[yAxis] [xAxis]" +// Valid yAxis values: 'top', 'middle', 'bottom' +// Valid xAxis values: 'left', 'center', 'right' +const AVAILABLE_POSITIONS = [ + 'top left', + 'top center', + 'top right', + 'middle left', + 'middle center', + 'middle right', + 'bottom left', + 'bottom center', + 'bottom right', +]; + +// Follows floating UI's conventions +// See https://floating-ui.com/docs/computePosition#placement +const AVAILABLE_PLACEMENTS = [ + 'top', + 'top-start', + 'top-end', + 'right', + 'right-start', + 'right-end', + 'bottom', + 'bottom-start', + 'bottom-end', + 'left', + 'left-start', + 'left-end', +]; export default { title: 'Components/Popover', component: Popover, - parameters: { - knobs: { disable: false }, - }, -}; - -export const _default = () => { - const show = boolean( 'Example: Show', true ); - const children = text( 'children', 'Popover Content' ); - const animate = boolean( 'animate', false ); - const expandOnMobile = boolean( 'expandOnMobile', false ); - const focusOnMount = select( - 'focusOnMount', - { - firstElement: 'firstElement', - container: 'container', + argTypes: { + anchorRef: { control: { type: null } }, + anchorRect: { control: { type: null } }, + animate: { control: { type: 'boolean' } }, + children: { control: { type: null } }, + className: { control: { type: 'text' } }, + expandOnMobile: { control: { type: 'boolean' } }, + focusOnMount: { + control: { type: 'select' }, + options: [ 'firstElement', 'container', false ], }, - 'firstElement' - ); - const noArrow = boolean( 'noArrow', false ); - const isAlternate = boolean( 'isAlternate', false ); - - const props = { - animate, - children, - expandOnMobile, - focusOnMount, - noArrow, - isAlternate, - }; - - if ( ! show ) { - return null; - } - - return ; + getAnchorRect: { control: { type: null } }, + headerTitle: { control: { type: 'text' } }, + isAlternate: { control: { type: 'boolean' } }, + noArrow: { control: { type: 'boolean' } }, + onClose: { control: { type: null } }, + offset: { control: { type: 'number' } }, + onFocusOutside: { control: { type: null } }, + placement: { + control: { type: 'select' }, + options: AVAILABLE_PLACEMENTS, + }, + position: { + control: { type: 'select' }, + options: AVAILABLE_POSITIONS, + }, + __unstableSlotName: { control: { type: null } }, + __unstableObserveElement: { control: { type: null } }, + __unstableForcePosition: { control: { type: 'boolean' } }, + __unstableShift: { control: { type: 'boolean' } }, + }, }; -const DragExample = ( props ) => { - const { label, content, ...restProps } = props; +const PopoverWithAnchor = ( args ) => { + const anchorRef = useRef( null ); return ( -
-
-

Move the gray box around.

-

- The{ ' ' } - - pink bordered - { ' ' } - element is a parent. -

-

- The{ ' ' } - - cyan bordered - { ' ' } - element is a sibling to Popover. -

-

- Popover aligns to the content within - parent. -

-
-
+

- -

{ label }
- { content } - -
+ Popover's anchor +

+
); }; -export const positioning = () => { - const label = text( 'Example: Label', 'Drag Me!' ); - const content = text( 'Example: Content', 'Popover' ); - const noArrow = boolean( 'noArrow', false ); +export const Default = ( args ) => { + const [ isVisible, setIsVisible ] = useState( false ); + const toggleVisible = () => { + setIsVisible( ( state ) => ! state ); + }; return ( - +
+ +
); }; +Default.args = { + children: <>Popover's content, +}; -function DynamicHeightPopover() { +/** + * Resize / scroll the viewport to test the behavior of the popovers when they + * reach the viewport boundaries. + */ +export const AllPlacements = ( { children, ...args } ) => ( +
+

+ Resize / scroll the viewport to test the behavior of the popovers + when they reach the viewport boundaries. +

+
+ { AVAILABLE_PLACEMENTS.map( ( p ) => ( + + { children } +
+ (placement: { p }) +
+
+ ) ) } +
+
+); +// Excluding placement and position since they all possible values +// are passed directly in code. +AllPlacements.parameters = { + controls: { + exclude: [ 'placement', 'position' ], + }, +}; +AllPlacements.args = { + ...Default.args, + noArrow: false, + offset: 10, +}; + +export const DynamicHeight = ( { children, ...args } ) => { const [ height, setHeight ] = useState( 200 ); const increase = () => setHeight( height + 100 ); const decrease = () => setHeight( height - 100 ); @@ -146,7 +199,7 @@ function DynamicHeightPopover() {

- +
- Content with dynamic height + { children }
); -} +}; +DynamicHeight.args = { + ...Default.args, + children: 'Content with dynamic height', +}; + +export const WithSlotOutsideIframe = ( args ) => { + const anchorRef = useRef( null ); + const slotName = 'popover-with-slot-outside-iframe'; -export const dynamicHeight = () => { - return ; + return ( + +
+ + +
+
+ ); +}; +WithSlotOutsideIframe.args = { + ...Default.args, }; diff --git a/packages/components/src/popover/style.scss b/packages/components/src/popover/style.scss index 00639f67216781..13649142c2caec 100644 --- a/packages/components/src/popover/style.scss +++ b/packages/components/src/popover/style.scss @@ -73,9 +73,9 @@ $arrow-triangle-base-size: 14px; content: ""; position: absolute; top: -1px; - left: 0; + left: 1px; height: 2px; - width: 100%; + right: 1px; background-color: $white; }