-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
InlineTip: Use Modal instead of window.confirm()
- Loading branch information
1 parent
609a09c
commit b1270ca
Showing
7 changed files
with
124 additions
and
86 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
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,40 +1,80 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Notice } from '@wordpress/components'; | ||
import { useState } from '@wordpress/element'; | ||
import { Notice, Modal, Button } from '@wordpress/components'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export function InlineTip( { isVisible, className, onRemove, children } ) { | ||
if ( ! isVisible ) { | ||
return null; | ||
} | ||
export function InlineTip( { | ||
children, | ||
className, | ||
isTipVisible, | ||
onDisableTips, | ||
onDismissTip, | ||
} ) { | ||
const [ isConfirmationVisible, setIsConfirmationVisible ] = useState( false ); | ||
|
||
const openConfirmation = () => setIsConfirmationVisible( true ); | ||
const closeConfirmation = () => setIsConfirmationVisible( false ); | ||
|
||
const dismissTip = () => { | ||
onDismissTip(); | ||
closeConfirmation(); | ||
}; | ||
|
||
const disableTips = () => { | ||
onDisableTips(); | ||
closeConfirmation(); | ||
}; | ||
|
||
return ( | ||
<Notice className={ className } onRemove={ onRemove }> | ||
{ children } | ||
</Notice> | ||
<> | ||
{ isTipVisible && ( | ||
<Notice | ||
className={ classnames( 'nux-inline-tip', className ) } | ||
onRemove={ openConfirmation } | ||
> | ||
{ children } | ||
</Notice> | ||
) } | ||
|
||
{ isConfirmationVisible && ( | ||
<Modal | ||
className="nux-hide-tips-confirmation" | ||
title={ __( 'Hide Tips' ) } | ||
onRequestClose={ closeConfirmation } | ||
> | ||
{ __( 'Would you like to disable tips like these in the future?' ) } | ||
<div className="nux-hide-tips-confirmation__buttons"> | ||
<Button isDefault isLarge onClick={ dismissTip }> | ||
{ __( 'No' ) } | ||
</Button> | ||
<Button isPrimary isLarge onClick={ disableTips }> | ||
{ __( 'Disable Tips' ) } | ||
</Button> | ||
</div> | ||
</Modal> | ||
) } | ||
</> | ||
); | ||
} | ||
|
||
export default compose( | ||
withSelect( ( select, { tipId } ) => ( { | ||
isVisible: select( 'core/nux' ).isTipVisible( tipId ), | ||
isTipVisible: select( 'core/nux' ).isTipVisible( tipId ), | ||
} ) ), | ||
withDispatch( ( dispatch, { tipId } ) => { | ||
const { disableTips, dismissTip } = dispatch( 'core/nux' ); | ||
|
||
return { | ||
onRemove() { | ||
// Disable reason: We don't yet have a <Confirm> component. One day! | ||
// eslint-disable-next-line no-alert | ||
if ( window.confirm( __( 'Would you like to disable tips like these in the future? ' ) ) ) { | ||
disableTips(); | ||
} else { | ||
dismissTip( tipId ); | ||
} | ||
}, | ||
onDismissTip: () => dismissTip( tipId ), | ||
onDisableTips: disableTips, | ||
}; | ||
} ) | ||
)( InlineTip ); |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.nux-hide-tips-confirmation .components-modal__header { | ||
border-bottom: none; | ||
margin-bottom: 0; | ||
} | ||
|
||
.nux-hide-tips-confirmation__buttons { | ||
margin-top: 2rem; | ||
text-align: right; | ||
|
||
.components-button { | ||
margin-left: 10px; | ||
} | ||
} |
34 changes: 6 additions & 28 deletions
34
packages/nux/src/components/inline-tip/test/__snapshots__/index.js.snap
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,34 +1,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`InlineTip should render correctly 1`] = ` | ||
<div | ||
className="components-notice is-undefined is-dismissible" | ||
> | ||
<div | ||
className="components-notice__content" | ||
<Fragment> | ||
<Notice | ||
className="nux-inline-tip" | ||
onRemove={[Function]} | ||
> | ||
It looks like you’re writing a letter. Would you like help? | ||
</div> | ||
<button | ||
aria-label="Dismiss this notice" | ||
className="components-button components-icon-button components-notice__dismiss" | ||
onClick={[Function]} | ||
type="button" | ||
> | ||
<svg | ||
aria-hidden="true" | ||
className="dashicon dashicons-no" | ||
focusable="false" | ||
height={20} | ||
role="img" | ||
viewBox="0 0 20 20" | ||
width={20} | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
d="M12.12 10l3.53 3.53-2.12 2.12L10 12.12l-3.54 3.54-2.12-2.12L7.88 10 4.34 6.46l2.12-2.12L10 7.88l3.54-3.53 2.12 2.12z" | ||
/> | ||
</svg> | ||
</button> | ||
</div> | ||
</Notice> | ||
</Fragment> | ||
`; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
@import "./components/dot-tip/style.scss"; | ||
@import "./components/inline-tip/style.scss"; |