-
Notifications
You must be signed in to change notification settings - Fork 1
Add Related Items slot (#64) #119
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
Open
Cabonie
wants to merge
2
commits into
main
Choose a base branch
from
64-related-items-slot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| --- | ||
| myst: | ||
| html_meta: | ||
| "description": "How to hide or replace the related items list shown below content in Plone Aurora" | ||
| "property=og:description": "How to hide or replace the related items list shown below content in Plone Aurora" | ||
| "property=og:title": "Customize the related items" | ||
| "keywords": "Plone Aurora, related items, slot, belowContent" | ||
| --- | ||
|
|
||
| # Customize the related items | ||
|
|
||
| Plone Aurora renders a content item's related items below the content as links. | ||
| The list comes from a slot component registered for the `belowContent` slot. | ||
| This guide shows you how to hide or replace it. | ||
|
|
||
| ## Hide the related items | ||
|
|
||
| Set `showRelatedItems` to `false` in your add-on configuration to hide the list on every page. | ||
|
|
||
| ```ts | ||
| import type { ConfigType } from '@plone/registry'; | ||
|
|
||
| export default function install(config: ConfigType) { | ||
| config.settings.showRelatedItems = false; | ||
| return config; | ||
| } | ||
| ``` | ||
|
|
||
| The slot renders the list whenever an item has `relatedItems`, unless `showRelatedItems` is `false`. | ||
|
|
||
| ## Replace the related items component | ||
|
|
||
| Register your own component for the `belowContent` slot under the same name to override the default. | ||
|
|
||
| ```ts | ||
| import type { ConfigType } from '@plone/registry'; | ||
| import MyRelatedItems from './MyRelatedItems'; | ||
|
|
||
| export default function install(config: ConfigType) { | ||
| config.registerSlotComponent({ | ||
| name: 'RelatedItems', | ||
| slot: 'belowContent', | ||
| component: MyRelatedItems, | ||
| }); | ||
| return config; | ||
| } | ||
| ``` | ||
|
|
||
| Your component receives the slot props, including `content`, from which you can read `content.relatedItems`. | ||
| To learn how slots work in general, refer to {doc}`register-slots`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added a Related Items slot that renders a content item's related items as links below the content. @Cabonie |
15 changes: 15 additions & 0 deletions
15
packages/layout/slots/RelatedItems/RelatedItems.module.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| @layer custom { | ||
| .relatedItems { | ||
| padding: 0; | ||
| margin-top: 1.5rem; | ||
| list-style: none; | ||
| } | ||
|
|
||
| .relatedItems li { | ||
| margin-bottom: 0.5rem; | ||
| } | ||
|
|
||
| .relatedItems li:last-child { | ||
| margin-bottom: 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { MemoryRouter } from 'react-router'; | ||
| import { RouterProvider } from 'react-aria-components'; | ||
| import RelatedItems from './RelatedItems'; | ||
|
|
||
| type Item = { '@id': string; title: string }; | ||
|
|
||
| const renderRelatedItems = (relatedItems: Item[]) => | ||
| render( | ||
| <MemoryRouter> | ||
| <RouterProvider navigate={() => undefined}> | ||
| <RelatedItems | ||
| content={{ relatedItems } as never} | ||
| location={{ pathname: '/' } as never} | ||
| /> | ||
| </RouterProvider> | ||
| </MemoryRouter>, | ||
| ); | ||
|
|
||
| describe('RelatedItems slot', () => { | ||
| it('renders nothing when there are no related items', () => { | ||
| const { container } = renderRelatedItems([]); | ||
| expect(container).toBeEmptyDOMElement(); | ||
| }); | ||
|
|
||
| it('renders a link for each related item', () => { | ||
| renderRelatedItems([ | ||
| { '@id': '/news/one', title: 'News One' }, | ||
| { '@id': '/events/two', title: 'Event Two' }, | ||
| ]); | ||
| expect(screen.getByRole('link', { name: 'News One' })).toHaveAttribute( | ||
| 'href', | ||
| '/news/one', | ||
| ); | ||
| expect(screen.getByRole('link', { name: 'Event Two' })).toHaveAttribute( | ||
| 'href', | ||
| '/events/two', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { Link } from '@plone/components'; | ||
| import config from '@plone/registry'; | ||
| import type { SlotComponentProps } from '../SlotRenderer'; | ||
| import SectionWrapper from '../../components/SectionWrapper/SectionWrapper'; | ||
| import styles from './RelatedItems.module.css'; | ||
|
|
||
| const RelatedItems = (props: SlotComponentProps) => { | ||
| const { t } = useTranslation(); | ||
| const items = props.content.relatedItems ?? []; | ||
|
|
||
| const showRelatedItems = config.settings.showRelatedItems !== false; | ||
| if (!showRelatedItems || items.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <SectionWrapper | ||
| as="nav" | ||
| width="layout" | ||
| aria-label={t('layout.relatedItems.label')} | ||
| > | ||
| <ul className={styles.relatedItems}> | ||
| {items.map((item) => ( | ||
| <li key={item['@id']}> | ||
| <Link href={item['@id']}>{item.title}</Link> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </SectionWrapper> | ||
| ); | ||
| }; | ||
|
|
||
| export default RelatedItems; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sneridagh same as @arybakov05 said in #112 : do you think we should keep these configs or just instruct people to use slots configuration? I think I prefer the latter but I would like to hear your thoughs.