Skip to content
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

Placeholder: Add an illustration option to the Placeholder component #41605

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import namesPlugin from 'colord/plugins/names';
*/
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useEffect, useRef } from '@wordpress/element';
import { Spinner } from '@wordpress/components';
import { Placeholder, Spinner } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import {
withColors,
Expand All @@ -24,7 +24,6 @@ import {
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { isBlobURL } from '@wordpress/blob';
import { SVG, Path } from '@wordpress/primitives';
import { store as noticesStore } from '@wordpress/notices';

/**
Expand All @@ -47,18 +46,6 @@ import ResizableCover from './resizable-cover';

extend( [ namesPlugin ] );

const placeholderIllustration = (
<SVG
className="components-placeholder__illustration"
fill="none"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 60 60"
preserveAspectRatio="none"
>
<Path vectorEffect="non-scaling-stroke" d="M60 60 0 0" />
</SVG>
);

function getInnerBlocksTemplate( attributes ) {
return [
[
Expand Down Expand Up @@ -360,9 +347,10 @@ function CoverEdit( {
) }

{ ! url && useFeaturedImage && (
<div className="wp-block-cover__image--placeholder-image">
{ placeholderIllustration }
</div>
<Placeholder
className="wp-block-cover__image--placeholder-image"
withIllustration={ true }
/>
) }

{ url &&
Expand Down
13 changes: 0 additions & 13 deletions packages/block-library/src/cover/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,6 @@
right: 0;
bottom: 0;
left: 0;

// Style the placeholder illustration.
.components-placeholder__illustration {
position: absolute;
left: 0;
width: 100%;
height: 100%;
stroke: currentColor;
stroke-dasharray: 3;
opacity: 0.4;
}

border: $border-width dashed currentColor;
}

}
Expand Down
44 changes: 14 additions & 30 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,25 @@ import {
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { upload } from '@wordpress/icons';
import { SVG, Path } from '@wordpress/primitives';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
*/
import DimensionControls from './dimension-controls';

const placeholderIllustration = (
<SVG
className="components-placeholder__illustration"
fill="none"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 60 60"
preserveAspectRatio="none"
>
<Path vectorEffect="non-scaling-stroke" d="M60 60 0 0" />
</SVG>
);

const ALLOWED_MEDIA_TYPES = [ 'image' ];
const placeholderChip = (
<div className="wp-block-post-featured-image__placeholder">
{ placeholderIllustration }
</div>
);

const placeholder = ( content ) => {
return (
<Placeholder
className="block-editor-media-placeholder"
withIllustration={ true }
>
{ content }
</Placeholder>
);
};

function getMediaSourceUrlBySizeSlug( media, slug ) {
return (
Expand Down Expand Up @@ -101,15 +94,6 @@ function PostFeaturedImageDisplay( {
style: { width, height },
} );

const placeholder = ( content ) => {
return (
<Placeholder className="block-editor-media-placeholder">
{ placeholderIllustration }
{ content }
</Placeholder>
);
};

const onSelectImage = ( value ) => {
if ( value?.id ) {
setFeaturedImage( value.id );
Expand Down Expand Up @@ -153,7 +137,7 @@ function PostFeaturedImageDisplay( {
return (
<>
{ controls }
<div { ...blockProps }>{ placeholderChip }</div>
<div { ...blockProps }>{ placeholder() }</div>
</>
);
}
Expand Down Expand Up @@ -187,7 +171,7 @@ function PostFeaturedImageDisplay( {
} else {
// We have a Featured image so show a Placeholder if is loading.
image = ! media ? (
placeholderChip
placeholder()
) : (
<img
src={ mediaUrl }
Expand Down Expand Up @@ -232,7 +216,7 @@ function PostFeaturedImageDisplay( {
export default function PostFeaturedImageEdit( props ) {
const blockProps = useBlockProps();
if ( ! props.context?.postId ) {
return <div { ...blockProps }>{ placeholderChip }</div>;
return <div { ...blockProps }>{ placeholder() }</div>;
}
return <PostFeaturedImageDisplay { ...props } />;
}
29 changes: 0 additions & 29 deletions packages/block-library/src/post-featured-image/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@
// against any background color.
color: currentColor;
background: transparent;
&::before {
content: "";
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border: $border-width dashed currentColor;
opacity: 0.4;
pointer-events: none;

// Inherit border radius from style variations.
border-radius: inherit;
}

// Style the upload button.
.components-placeholder__fieldset {
Expand Down Expand Up @@ -82,20 +67,6 @@
color: $white;
}

// Style the placeholder illustration.
.components-placeholder__illustration {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
stroke: currentColor;
stroke-dasharray: 3;
opacity: 0.4;
}

// Show default placeholder height when not resized.
min-height: 200px;
}
Expand Down
15 changes: 1 addition & 14 deletions packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { crop, upload } from '@wordpress/icons';
import { SVG, Path } from '@wordpress/primitives';
import { store as noticesStore } from '@wordpress/notices';

/**
Expand Down Expand Up @@ -520,20 +519,8 @@ export default function LogoEdit( {
<Placeholder
className={ placeholderClassName }
preview={ logoImage }
withIllustration={ true }
>
{
<SVG
className="components-placeholder__illustration"
fill="none"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 60 60"
>
<Path
vectorEffect="non-scaling-stroke"
d="m61 32.622-13.555-9.137-15.888 9.859a5 5 0 0 1-5.386-.073l-9.095-5.989L1 37.5"
/>
</SVG>
}
{ content }
</Placeholder>
);
Expand Down
14 changes: 0 additions & 14 deletions packages/block-library/src/site-logo/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,6 @@
.components-button.components-button > svg {
color: $white;
}

// Style the placeholder illustration.
.components-placeholder__illustration {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
stroke: currentColor;
stroke-dasharray: 3;
opacity: 0.4;
}
}

// Show upload button on block selection.
Expand Down
13 changes: 7 additions & 6 deletions packages/components/src/placeholder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const MyPlaceholder = () => <Placeholder icon={ more } label="Placeholder" />;

### Props

| Name | Type | Default | Description |
| ---------------- | ------------------- | ----------- | ----------------------------------------------------------------- |
| `icon` | `string, WPElement` | `undefined` | If provided, renders an icon next to the label. |
| `label` | `string` | `undefined` | Renders a label for the placeholder. |
| `instructions` | `string` | `undefined` | Renders instruction text below label. |
| `isColumnLayout` | `bool` | `false` | Changes placeholder children layout from flex-row to flex-column. |
| Name | Type | Default | Description |
| ---------------- | ------------------- | ----------- | ----------------------------------------------------------------- |
| `icon` | `string, WPElement` | `undefined` | If provided, renders an icon next to the label. |
| `label` | `string` | `undefined` | Renders a label for the placeholder. |
| `instructions` | `string` | `undefined` | Renders instruction text below label. |
| `isColumnLayout` | `bool` | `false` | Changes placeholder children layout from flex-row to flex-column. |
| `withIllustration`| `bool` | `false` | Outputs a placeholder illustration. |
37 changes: 27 additions & 10 deletions packages/components/src/placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,38 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { useResizeObserver } from '@wordpress/compose';
import { SVG, Path } from '@wordpress/primitives';

/**
* Internal dependencies
*/
import Icon from '../icon';

const PlaceholderIllustration = (
<SVG
className="components-placeholder__illustration"
fill="none"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 60 60"
preserveAspectRatio="none"
>
<Path vectorEffect="non-scaling-stroke" d="M60 60 0 0" />
</SVG>
);

/**
* Renders a placeholder. Normally used by blocks to render their empty state.
*
* @param {Object} props The component props.
* @param {WPIcon} props.icon An icon rendered before the label.
* @param {WPElement} props.children Children to be rendered.
* @param {string} props.label Title of the placeholder.
* @param {string} props.instructions Instructions of the placeholder.
* @param {string} props.className Class to set on the container div.
* @param {Object} props.notices A rendered notices list.
* @param {Object} props.preview Preview to be rendered in the placeholder.
* @param {boolean} props.isColumnLayout Whether a column layout should be used.
* @param {Object} props The component props.
* @param {WPIcon} props.icon An icon rendered before the label.
* @param {WPElement} props.children Children to be rendered.
* @param {string} props.label Title of the placeholder.
* @param {string} props.instructions Instructions of the placeholder.
* @param {string} props.className Class to set on the container div.
* @param {Object} props.notices A rendered notices list.
* @param {Object} props.preview Preview to be rendered in the placeholder.
* @param {boolean} props.isColumnLayout Whether a column layout should be used.
* @param {boolean} props.withIllustration Whether to add an illustration to the placeholder.
*
* @return {Object} The rendered placeholder.
*/
Expand All @@ -37,6 +51,7 @@ function Placeholder( {
notices,
preview,
isColumnLayout,
withIllustration,
...additionalProps
} ) {
const [ resizeListener, { width } ] = useResizeObserver();
Expand All @@ -55,13 +70,15 @@ function Placeholder( {
const classes = classnames(
'components-placeholder',
className,
modifierClassNames
modifierClassNames,
withIllustration ? 'has-illustration' : null
);
const fieldsetClasses = classnames( 'components-placeholder__fieldset', {
'is-column-layout': isColumnLayout,
} );
return (
<div { ...additionalProps } className={ classes }>
{ withIllustration ? PlaceholderIllustration : null }
{ resizeListener }
{ notices }
{ preview && (
Expand Down
23 changes: 23 additions & 0 deletions packages/components/src/placeholder/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
.components-base-control__label {
font-size: $default-font-size;
}

&.has-illustration {
background: none;
border: none;
box-shadow: none;
min-width: 100px;
}
}

.components-placeholder__error,
Expand Down Expand Up @@ -171,3 +178,19 @@
}
}
}

// Style the placeholder illustration.
.components-placeholder__illustration {
border: $border-width dashed currentColor;
box-sizing: border-box;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
stroke: currentColor;
stroke-dasharray: 3;
opacity: 0.4;
}