Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/iconography/src/block/Attributes.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export type Attributes = Partial< {
iconClass: WPFormat[ 'className' ];
iconTag: WPFormat[ 'tagName' ];
iconContent: string;
ariaLabel?: string;
ariaHidden?: boolean;
} >;
41 changes: 38 additions & 3 deletions packages/iconography/src/block/Edit.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import React from 'react';

/* WordPress Dependencies */
import { useBlockProps } from '@wordpress/block-editor';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { store as RichTextStore } from '@wordpress/rich-text';
import { useSelect } from '@wordpress/data';
import { Icon, Spinner } from '@wordpress/components';
import {
Icon,
Spinner,
PanelBody,
TextControl,
ToggleControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { replace } from '@wordpress/icons';

/* Internal deps */
import { IconToolbarButton } from '../shared';
import { toTitleCase } from '../utils';
import './style.scss';

/* Types */
Expand All @@ -22,6 +29,7 @@ export const Edit = ( {
attributes,
setAttributes,
}: BlockEditProps< Attributes > ) => {
const { ariaLabel, ariaHidden } = attributes;
const blockProps = useBlockProps();
const { getFormatType } = useSelect(
( select ) =>
Expand All @@ -48,9 +56,32 @@ export const Edit = ( {

const TagName =
( attributes.iconTag as keyof HTMLElementTagNameMap ) ?? 'span';
const defaultAriaLabel = attributes.iconContent
? toTitleCase( attributes.iconContent ) + ' icon'
: undefined;

return (
<>
<InspectorControls>
<PanelBody title={ __( 'ARIA Settings' ) }>
<TextControl
label={ __( 'ARIA Label' ) }
value={ ariaLabel ?? '' }
onChange={ ( value ) =>
setAttributes( { ariaLabel: value } )
}
/>
<ToggleControl
label={ __(
'Hide element from assistive technologies (aria-hidden)'
) }
checked={ ariaHidden }
onChange={ ( value ) =>
setAttributes( { ariaHidden: value } )
}
/>
</PanelBody>
</InspectorControls>
<IconToolbarButton
icon={ <Icon icon={ replace } /> }
onChange={ handleChange }
Expand All @@ -65,7 +96,11 @@ export const Edit = ( {
/>
<div { ...blockProps }>
{ attributes.iconContent && (
<TagName className={ attributes.iconClass ?? '' }>
<TagName
className={ attributes.iconClass ?? '' }
aria-label={ ariaLabel || defaultAriaLabel }
aria-hidden={ ariaHidden || undefined }
>
{ attributes.iconContent }
</TagName>
) }
Expand Down
17 changes: 15 additions & 2 deletions packages/iconography/src/block/Save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@ import React from 'react';
/* WordPress Dependencies */
import { useBlockProps } from '@wordpress/block-editor';

/* Internal deps */
import { toTitleCase } from '../utils';

/* Types */
import type { Attributes } from './Attributes.type';
import type { BlockSaveProps } from '@wordpress/blocks';

export const Save = ( { attributes }: BlockSaveProps< Attributes > ) => {
const { iconClass, iconTag, iconContent } = attributes;
const { iconClass, iconTag, iconContent, ariaHidden, ariaLabel } =
attributes;
const TagName = ( iconTag as keyof HTMLElementTagNameMap ) ?? 'span';
const defaultAriaLabel = attributes.iconContent
? toTitleCase( attributes.iconContent ) + ' icon'
: undefined;

return (
<div { ...useBlockProps.save() }>
<TagName className={ iconClass ?? '' }>{ iconContent }</TagName>
<TagName
className={ iconClass ?? '' }
aria-label={ ariaLabel || defaultAriaLabel }
aria-hidden={ ariaHidden || undefined }
>
{ iconContent }
</TagName>
</div>
);
};
14 changes: 13 additions & 1 deletion packages/iconography/src/block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"category": "media",
"description": "Display an icon as a block.",
"icon": "star-filled",
"keywords": [ "icon", "emoji", "symbol" ],
"keywords": [
"icon",
"emoji",
"symbol"
],
"textdomain": "boxuk",
"style": "file:./style-index.css",
"editorScript": "file:./index.ts",
Expand Down Expand Up @@ -69,6 +73,14 @@
"default": {
"textAlign": "center"
}
},
"ariaLabel": {
"type": "string",
"default": ""
},
"ariaHidden": {
"type": "boolean",
"default": ""
}
},
"example": {}
Expand Down
12 changes: 12 additions & 0 deletions packages/iconography/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ export const getIconGroups = ( withEdit = true ): IconGroup[] | undefined =>
edit: () => {},
};
} );

/**
*
* @param {string} title The title to change to title case.
* @return {string} The altered title.
*/
export function toTitleCase( title: string ): string {
title = title.replace( /_/g, ' ' );
title =
title.charAt( 0 ).toUpperCase() + title.substring( 1 ).toLowerCase();
return title;
}