Skip to content

Commit

Permalink
Proof of Concept
Browse files Browse the repository at this point in the history
  • Loading branch information
fullofcaffeine committed Aug 20, 2021
1 parent b2b35ba commit 72cd96b
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,12 @@
"markdown_source": "../packages/components/src/combobox-control/README.md",
"parent": "components"
},
{
"title": "Confirm",
"slug": "confirm",
"markdown_source": "../packages/components/src/confirm/README.md",
"parent": "components"
},
{
"title": "CustomSelectControl",
"slug": "custom-select-control",
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"moment": "^2.22.1",
"re-resizable": "^6.4.0",
"react-colorful": "^5.3.0",
"react-confirm": "0.1.24",
"react-dates": "^17.1.1",
"react-resize-aware": "^3.1.0",
"react-use-gesture": "^9.0.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/confirm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `Confirm`

TBD
63 changes: 63 additions & 0 deletions packages/components/src/confirm/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// @ts-nocheck (for now)

/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import type { Ref } from 'react';
import { confirmable } from 'react-confirm';

/**
* Internal dependencies
*/
import Button from '../button';
import type { OwnProps } from './types';
import { Card, CardHeader, CardFooter } from '../card';
import { Heading } from '../heading';
import { contextConnect, PolymorphicComponentProps } from '../ui/context';
import { useConfirm } from './hook';

// @todo deal with overlay click event, close dialog
// @todo add type declarations for the react-confirm functions
function Confirm(
props: PolymorphicComponentProps< OwnProps, 'div', false >,
forwardedRef: Ref< any >
) {
const {
role,
wrapperClassName,
show,
proceed,
confirmation,
...otherProps
} = useConfirm( props );

return (
<div
{ ...otherProps }
role={ role }
className={ wrapperClassName }
ref={ forwardedRef }
style={ { visibility: show ? 'visible' : 'hidden' } }
>
<Card>
<CardHeader>
<Heading level="4">{ confirmation }</Heading>
</CardHeader>
<CardFooter justify="center">
<Button
variant="secondary"
onClick={ () => proceed( false ) }
>
Cancel
</Button>
<Button variant="primary" onClick={ () => proceed( true ) }>
OK
</Button>
</CardFooter>
</Card>
</div>
);
}

export default confirmable( contextConnect( Confirm, 'Confirm' ) );
32 changes: 32 additions & 0 deletions packages/components/src/confirm/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Internal dependencies
*/
import { useContextSystem, PolymorphicComponentProps } from '../ui/context';

/**
* Internal dependencies
*/
import * as styles from './styles';
import { useCx } from '../utils/hooks/use-cx';
import type { OwnProps } from './types';

export function useConfirm(
props: PolymorphicComponentProps< OwnProps, 'div' >
) {
const { className, ...otherProps } = useContextSystem( props, 'Confirm' );

const cx = useCx();

const classes = cx( className );
const wrapperClassName = cx( styles.overlayWrapper );
const dialogWrapperClassName = cx( styles.dialogWrapper );

console.log( otherProps );

return {
className: classes,
wrapperClassName,
dialogWrapperClassName,
...otherProps,
};
}
15 changes: 15 additions & 0 deletions packages/components/src/confirm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ts-nocheck

/**
* External dependencies
*/
import { createConfirmation } from 'react-confirm';

/**
* Internal dependencies
*/
import Confirm from './component';

const confirm = createConfirmation( Confirm );

export { Confirm, confirm };
18 changes: 18 additions & 0 deletions packages/components/src/confirm/stories/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* External dependencies
*/
import React from 'react';

/**
* Internal dependencies
*/
import { Confirm } from '..';

export default {
component: Confirm,
title: 'Components (Experimental)/Confirm',
};

export const _default = () => {
return <Confirm />;
};
24 changes: 24 additions & 0 deletions packages/components/src/confirm/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* External dependencies
*/
import { css } from '@emotion/react';

export const overlayWrapper = css`
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 99;
background: rgba( 0, 0, 0, 0.5 );
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
justify-content: center;
-ms-align-items: center;
align-items: center;
`;

export const dialogWrapper = css``;
1 change: 1 addition & 0 deletions packages/components/src/confirm/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TBD
10 changes: 10 additions & 0 deletions packages/components/src/confirm/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Internal dependencies
*/

// TBD
export interface OwnProps {
show: boolean;
proceed: Function;
confirmation: string;
}
1 change: 1 addition & 0 deletions packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,4 @@ export {
} from './higher-order/with-focus-return';
export { default as withNotices } from './higher-order/with-notices';
export { default as withSpokenMessages } from './higher-order/with-spoken-messages';
export { confirm } from './confirm'; // @todo make it experimental
1 change: 1 addition & 0 deletions packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"src/base-field/**/*",
"src/button/**/*",
"src/card/**/*",
"src/confirm/**/*",
"src/dashicon/**/*",
"src/disabled/**/*",
"src/divider/**/*",
Expand Down
12 changes: 7 additions & 5 deletions packages/editor/src/components/post-visibility/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { VisuallyHidden } from '@wordpress/components';
import { VisuallyHidden, confirm } from '@wordpress/components';
import { withInstanceId, compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';

Expand Down Expand Up @@ -34,12 +34,14 @@ export class PostVisibility extends Component {
this.setState( { hasPassword: false } );
}

setPrivate() {
async setPrivate() {
if (
// eslint-disable-next-line no-alert
! window.confirm(
__( 'Would you like to privately publish this post now?' )
)
! ( await confirm( {
confirmation: __(
'Would you like to privately publish this post now?'
),
} ) )
) {
return;
}
Expand Down

0 comments on commit 72cd96b

Please sign in to comment.