-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Components: Draggable, add story #18070
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,69 @@ | ||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* WordPress dependencies | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
import { useState } from '@wordpress/element'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* Internal dependencies | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
import Draggable from '../'; | ||||||||||||||||||||||||
import Dashicon from '../../dashicon'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export default { title: 'Draggable', component: Draggable }; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const Box = ( props ) => { | ||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||
<div | ||||||||||||||||||||||||
{ ...props } | ||||||||||||||||||||||||
style={ { | ||||||||||||||||||||||||
alignItems: 'center', | ||||||||||||||||||||||||
display: 'flex', | ||||||||||||||||||||||||
justifyContent: 'center', | ||||||||||||||||||||||||
width: 100, | ||||||||||||||||||||||||
height: 100, | ||||||||||||||||||||||||
background: '#ddd', | ||||||||||||||||||||||||
} } | ||||||||||||||||||||||||
/> | ||||||||||||||||||||||||
); | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const DraggalbeExample = () => { | ||||||||||||||||||||||||
const [ isDragging, setDragging ] = useState( false ); | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to documentation, the function exported as a story isn't a React component. It means that we shouldn't use hooks in this context as they might stop to work in the near future if they change the way all that works. The easiest way to get rid of this warning is to move the part which manages state out of this function. See a similar usage: gutenberg/packages/components/src/checkbox-control/stories/index.js Lines 18 to 28 in a6e1252
In this case, it might be more work to do it :( |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
// Allow for the use of ID in the example | ||||||||||||||||||||||||
/* eslint-disable no-restricted-syntax */ | ||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||
<div> | ||||||||||||||||||||||||
<p>Is Dragging? { isDragging ? 'Yes' : 'No' }</p> | ||||||||||||||||||||||||
<div id="draggable-example-box" style={ { display: 'inline-flex' } }> | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit concerned about the implementation of the It's completely unrelated to this PR, but I'd be in favor of opening a follow-up issue which makes it easier to use. In effect, we should seek ways to make the need to use Taking the example from README: import { Dashicon, Draggable, Panel, PanelBody } from '@wordpress/components';
const MyDraggable = ( { onDragStart, onDragEnd } ) => (
<div id="draggable-panel">
<Panel header="Draggable panel" >
<PanelBody>
<Draggable
elementId="draggable-panel"
transferData={ { } }
onDragStart={ onDragStart }
onDragEnd={ onDragEnd }
>
{
( { onDraggableStart, onDraggableEnd } ) => (
<Dashicon
icon="move"
onDragStart={ onDraggableStart }
onDragEnd={ onDraggableEnd }
draggable
/>
)
}
</Draggable>
</PanelBody>
</Panel>
</div>
); I would love us to explore whether it could be refactored to: import { Dashicon, Draggable, Panel, PanelBody } from '@wordpress/components';
const MyDraggable = ( { onDragStart, onDragEnd } ) => (
<Draggable
as={ Panel }
header="Draggable panel"
transferData={ { } }
onDragStart={ onDragStart }
onDragEnd={ onDragEnd }
>
{ ( { onDraggableStart, onDraggableEnd } ) => (
<PanelBody>
<Dashicon
icon="move"
onDragStart={ onDraggableStart }
onDragEnd={ onDraggableEnd }
draggable
/>
</PanelBody>
) }
</Draggable>
); /cc @nosolosw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is also a related issue with draggable elements as explained in: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@gziolo I noticed that too. I found it a bit unintuitive/restrictive in my first couple attempts of getting The above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||
<Draggable elementId="draggable-example-box"> | ||||||||||||||||||||||||
{ ( { onDraggableStart, onDraggableEnd } ) => { | ||||||||||||||||||||||||
const handleOnDragStart = ( event ) => { | ||||||||||||||||||||||||
setDragging( true ); | ||||||||||||||||||||||||
onDraggableStart( event ); | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
const handleOnDragEnd = ( event ) => { | ||||||||||||||||||||||||
setDragging( false ); | ||||||||||||||||||||||||
onDraggableEnd( event ); | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||
<Box | ||||||||||||||||||||||||
onDragStart={ handleOnDragStart } | ||||||||||||||||||||||||
onDragEnd={ handleOnDragEnd } | ||||||||||||||||||||||||
draggable | ||||||||||||||||||||||||
> | ||||||||||||||||||||||||
<Dashicon icon="move" /> | ||||||||||||||||||||||||
</Box> | ||||||||||||||||||||||||
); | ||||||||||||||||||||||||
} } | ||||||||||||||||||||||||
</Draggable> | ||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||
); | ||||||||||||||||||||||||
/* eslint-enable no-restricted-syntax */ | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export const _default = () => { | ||||||||||||||||||||||||
return <DraggalbeExample />; | ||||||||||||||||||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yet another reason to introduce a Box component which would work cross-platform :)