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

[ New Inline Format ] Introduce Inline Time Format for improved SEO and accessibility #67751

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
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
1 change: 1 addition & 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/format-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@wordpress/components": "*",
"@wordpress/compose": "*",
"@wordpress/data": "*",
"@wordpress/date": "*",
"@wordpress/element": "*",
"@wordpress/html-entities": "*",
"@wordpress/i18n": "*",
Expand Down
2 changes: 2 additions & 0 deletions packages/format-library/src/default-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { link } from './link';
import { strikethrough } from './strikethrough';
import { underline } from './underline';
import { textColor } from './text-color';
import { time } from './time';
import { subscript } from './subscript';
import { superscript } from './superscript';
import { keyboard } from './keyboard';
Expand All @@ -25,6 +26,7 @@ export default [
strikethrough,
underline,
textColor,
time,
subscript,
superscript,
keyboard,
Expand Down
1 change: 1 addition & 0 deletions packages/format-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
@import "./link/style.scss";
@import "./text-color/style.scss";
@import "./language/style.scss";
@import "./time/style.scss";
116 changes: 116 additions & 0 deletions packages/format-library/src/time/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
DateTimePicker,
Popover,
Button,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { RichTextToolbarButton } from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import { removeFormat, applyFormat, useAnchor } from '@wordpress/rich-text';
import { postDate as icon } from '@wordpress/icons';
import { dateI18n, getSettings } from '@wordpress/date';

const name = 'core/time';
const title = __( 'Time' );

export const time = {
name,
title,
tagName: 'time',
className: null,
edit: Edit,
attributes: {
datetime: 'datetime',
},
};

function Edit( { isActive, value, onChange, contentRef } ) {
const [ date, setDate ] = useState( new Date() );
const [ isPopoverVisible, setIsPopoverVisible ] = useState( false );
const togglePopover = () => setIsPopoverVisible( ( state ) => ! state );
const dateTimeSettings = getSettings();

const applyDate = () => {
const formattedDate = dateI18n( 'c', date );
const attributes = {
datetime: formattedDate,
};
onChange(
applyFormat( value, {
type: name,
attributes,
} )
);
setIsPopoverVisible( false );
};

return (
<>
<RichTextToolbarButton
icon={ icon }
label={ title }
title={ title }
onClick={ () => {
if ( isActive ) {
onChange( removeFormat( value, name ) );
} else {
togglePopover();
}
} }
isActive={ isActive }
role="menuitemcheckbox"
/>
{ isPopoverVisible && (
<InlineDateUI
contentRef={ contentRef }
onClose={ togglePopover }
onApply={ applyDate }
date={ date }
onChangeDate={ setDate }
dateTimeSettings={ dateTimeSettings }
/>
) }
</>
);
}

function InlineDateUI( {
contentRef,
onClose,
onApply,
date,
onChangeDate,
dateTimeSettings,
} ) {
const popoverAnchor = useAnchor( {
editableContentElement: contentRef.current,
settings: time,
} );

return (
<Popover
className="block-editor-format-toolbar__date-popover"
anchor={ popoverAnchor }
onClose={ onClose }
>
<DateTimePicker
is12Hour={ dateTimeSettings.formats.time }
currentDate={ date }
onChange={ onChangeDate }
startOfWeek={ dateTimeSettings.l10n.startOfWeek }
/>
<HStack alignment="right">
<Button
__next40pxDefaultSize
variant="primary"
text={ __( 'Apply' ) }
onClick={ onApply }
/>
</HStack>
</Popover>
);
}
6 changes: 6 additions & 0 deletions packages/format-library/src/time/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.block-editor-format-toolbar__date-popover {
.components-popover__content {
width: auto;
padding: 1rem;
}
}
Loading