diff --git a/.changeset/real-olives-bow.md b/.changeset/real-olives-bow.md new file mode 100644 index 000000000..c6e2270a3 --- /dev/null +++ b/.changeset/real-olives-bow.md @@ -0,0 +1,5 @@ +--- +'@cloudfour/patterns': minor +--- + +Refactor the Author component to work for article or presentation bylines diff --git a/src/components/author/author.stories.mdx b/src/components/author/author.stories.mdx index f4b43fd7a..8a5843ef4 100644 --- a/src/components/author/author.stories.mdx +++ b/src/components/author/author.stories.mdx @@ -1,37 +1,50 @@ import { Story, Canvas, Meta } from '@storybook/addon-docs'; import template from './author.twig'; +import ariannaImage from './demo/arianna.png'; +import danielleImage from './demo/danielle.png'; import gerardoImage from './demo/gerardo.png'; import tylerImage from './demo/tyler.png'; import meganImage from './demo/megan.png'; -const gerardo = { - name: 'Gerardo Rodriguez', - avatar: gerardoImage, - link: 'https://cloudfour.com/is/gerardo', -}; -const tyler = { - name: 'Tyler Sticka', - avatar: tylerImage, - link: 'https://cloudfour.com/is/tyler', -}; -const megan = { - name: 'Megan Notarte', - avatar: meganImage, - link: 'https://cloudfour.com/is/megnotarte', -}; +const allAuthors = [ + { + name: 'Arianna Chau', + avatar: ariannaImage, + link: 'https://cloudfour.com/is/arianna', + }, + { + name: 'Danielle Romo', + avatar: danielleImage, + link: 'https://cloudfour.com/is/danielle', + }, + { + name: 'Megan Notarte', + avatar: meganImage, + link: 'https://cloudfour.com/is/megnotarte', + }, + { + name: 'Tyler Sticka', + avatar: tylerImage, + link: 'https://cloudfour.com/is/tyler', + }, + { + name: 'Gerardo Rodriguez', + avatar: gerardoImage, + link: 'https://cloudfour.com/is/gerardo', + }, +]; +const authors = (count) => allAuthors.slice(0, count); + + {(args) => + template({ + authors: authors(args.count), + }) + } + + + +## With `meta` content + +You can use the `meta` property to add meta content, for example, the author title. - + {(args) => template({ - authors: [gerardo, tyler, megan].slice(0, args.count), + authors: authors(args.count), + meta: 'Front-end Designer', + }) + } + + + +## With `date` content + +If a date is desired for the meta content, use the `date` property with a +date object value. The Author template has extra logic to create a more +accessible user experience. + +**Note:** The `meta` property will override the `date` if both are set. + + + + {(args) => + template({ + authors: authors(args.count), + date: new Date('March 31, 2021'), + }) + } + + + +## Multiple authors + +Multiple authors are also supported. + + + + {(args) => + template({ + authors: authors(args.count), date: new Date('March 31, 2021'), }) } @@ -55,15 +116,24 @@ Displays an author, or set of authors, for article bylines. ## Template Properties -- `class`: Append a class to the root element. -- `authors`: Array of [author objects](https://timber.github.io/docs/reference/timber-user/#properties) of the type: +`class` (string): CSS class(es) to append to the root element - ```ts - { - name: string | () => string; - avatar: string | () => string; - link: string | () => string; - } - ``` +`authors` (array): Array of [author objects](https://timber.github.io/docs/reference/timber-user/#properties) of the type: + +```ts + { + name: string | () => string; + avatar: string | () => string; + link: string | () => string; + } +``` + +`author_prefix` (optional, string, default "By"): Used to create a more +accessible user experience by adding visually hidden text, prefixes the author name (e.g. "By Arianna Chau") + +`meta` (optional, string): Extra information to be rendered below the author name(s) + +`date` (optional, date object): Represents a publication date -- `date`: A date object representing the publication date +`date_prefix` (optional, string, default "Published on"): Used to create a more +accessible user experience by adding visually hidden text, prefixes the date value (e.g. "Published on March 31st, 2021") diff --git a/src/components/author/author.test.ts b/src/components/author/author.test.ts new file mode 100644 index 000000000..6c1908cdf --- /dev/null +++ b/src/components/author/author.test.ts @@ -0,0 +1,62 @@ +import path from 'path'; +import { withBrowser } from 'pleasantest'; +import { loadTwigTemplate } from '../../../test-utils'; + +/** Helper to load the Twig template file */ +const template = loadTwigTemplate(path.join(__dirname, './author.twig')); + +describe('Author', () => { + test( + 'more accessible experience for publish date', + withBrowser(async ({ utils, screen }) => { + await utils.injectHTML( + await template({ + // The avatar is not included because I couldn't figure out how + // to include it. For the purposes of this test, though, it is + // not important so I left it out. + authors: [ + { + name: 'Shakira Isabel Mebarak Ripoll', + link: 'https://www.shakira.com/', + }, + ], + date: new Date('March 31, 2021'), + date_prefix: 'Presented on', + }) + ); + + // Confirm a more accessible user experience + await screen.getByText('Presented on March 31st, 2021'); + }) + ); + + test( + 'meta is prioritized over date', + withBrowser(async ({ utils, screen }) => { + await utils.injectHTML( + await template({ + // The avatar is not included because I couldn't figure out how + // to include it. For the purposes of this test, though, it is + // not important so I left it out. + authors: [ + { + name: 'Shakira Isabel Mebarak Ripoll', + link: 'https://www.shakira.com/', + }, + ], + date: new Date('March 31, 2021'), + meta: 'Singer and songwriter', + }) + ); + + // Confirm the meta value is rendered + await screen.getByText('Singer and songwriter'); + + // Confirm the date is not rendered + const publishDate = await screen.queryByText( + 'Published on March 31st, 2021' + ); + expect(publishDate).toBeNull(); + }) + ); +}); diff --git a/src/components/author/author.twig b/src/components/author/author.twig index a037489c4..c605d24bb 100644 --- a/src/components/author/author.twig +++ b/src/components/author/author.twig @@ -1,4 +1,47 @@ +{# + If both the `meta` and `date` properties contain values, the `meta` value has + a higher priority and will override the `date` value. + + If rendering a date as meta data, it is recommended to use the `date` property. + There is extra template logic to create a more accessible user experience. +#} +{% set _meta_content %} + {% block meta_content %} + {% if meta %} +

{{ meta }}

+ {% elseif date %} +

+ {# This creates a more accessible, and less confusing, user experience #} + + {{ date_prefix|default('Published on') }} {{ date|date('F jS, Y') }} + + {# + The

+ {% endif %} + {% endblock %} +{% endset %} +
+ {# Avatar(s) #} {% embed '@cloudfour/objects/bunch/bunch.twig' with { class: 'o-media__object' } %} {% block content %} {% for author in authors %} @@ -6,18 +49,24 @@ {% endfor %} {% endblock %} {% endembed %} -
-
- Authored by + + {# Author content #} +
+ {# Author links #} +

+ {{ author_prefix|default("By") }} {% for author in authors %} {% if loop.last and loop.length > 1 %}and {% endif %} {{ author.name }} {%- if not loop.last and loop.length > 2 %},{% endif %} {% endfor %} -

-
- Published on - -
+

+ + {# Author meta content #} + {% if _meta_content|trim %} +
+ {{ _meta_content }} +
+ {% endif %}
diff --git a/src/components/author/demo/arianna.png b/src/components/author/demo/arianna.png new file mode 100644 index 000000000..769d71ef0 Binary files /dev/null and b/src/components/author/demo/arianna.png differ diff --git a/src/components/author/demo/danielle.png b/src/components/author/demo/danielle.png new file mode 100644 index 000000000..8b6c53aff Binary files /dev/null and b/src/components/author/demo/danielle.png differ