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

Block examples documentation #6560

Merged
merged 53 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
dccee83
the name of the property is blockSchema not schema
erral Dec 30, 2024
bea33fd
document the option
erral Dec 30, 2024
8cd8e6f
we use the term not
erral Dec 30, 2024
bfe8c56
document the SidebarPortal and SidebarPopup components as functional …
erral Dec 30, 2024
485a42c
document the SidebarPortal and SidebarPopup components as functional …
erral Dec 30, 2024
992d234
add first example of a block
erral Dec 30, 2024
8228d2d
docs
erral Dec 30, 2024
1ecbe10
more docs
erral Dec 30, 2024
12efde1
add the docs of custom schema and variations
erral Dec 30, 2024
d54d289
provide an example with variations and schema enhancer
erral Dec 30, 2024
476a36b
change folder names
erral Dec 30, 2024
9c09ad9
volto verssion
erral Dec 30, 2024
4d75f66
changelog
erral Dec 31, 2024
68c42a4
Update docs/source/blocks/examples/index.md
erral Jan 3, 2025
a75f94a
Update docs/source/blocks/examples/index.md
erral Jan 3, 2025
7907f50
Update docs/source/blocks/examples/index.md
erral Jan 3, 2025
13056b1
Update docs/source/blocks/examples/index.md
erral Jan 3, 2025
8eb9ea2
Update docs/source/blocks/examples/customviewvariationsandschemaenhan…
erral Jan 3, 2025
de133a9
Update docs/source/blocks/examples/customviewvariationsandschemaenhan…
erral Jan 3, 2025
5b70643
Update docs/source/blocks/examples/customviewvariationsandschemaenhan…
erral Jan 3, 2025
e507eed
Update docs/source/blocks/examples/customviewvariationsandschemaenhan…
erral Jan 3, 2025
3fc9aa2
Update docs/source/blocks/examples/customviewvariationsandschemaenhan…
erral Jan 3, 2025
6896154
Update packages/volto/news/6560.documentation
erral Jan 3, 2025
ff52fee
Update docs/source/blocks/examples/customschemaandview.md
erral Jan 3, 2025
ef09838
Update docs/source/blocks/examples/customschemaandview.md
erral Jan 3, 2025
f87d1b4
Apply suggestions from code review
erral Jan 3, 2025
aa274bc
rename files
erral Jan 3, 2025
9a56331
minor fixes
erral Jan 3, 2025
524f007
Merge branch 'main' into erral-review-docs
erral Jan 3, 2025
938ae9a
Update docs/source/blocks/settings.md
erral Jan 4, 2025
34e4423
Update docs/source/blocks/settings.md
erral Jan 4, 2025
158ec29
Apply suggestions from code review
stevepiercy Jan 4, 2025
9e34489
Clean up bad merge
stevepiercy Jan 4, 2025
682871d
trim code blocks
stevepiercy Jan 4, 2025
a7d2fc0
grammar
stevepiercy Jan 4, 2025
4e5b9e0
punctuation
stevepiercy Jan 4, 2025
d149cfe
grammar
stevepiercy Jan 4, 2025
739e588
s/webmaster/editor, one sentence per line, shorten intro
stevepiercy Jan 4, 2025
3d83b54
Move comment about HOC from code block to narrative text
stevepiercy Jan 4, 2025
bc5e436
Apply suggestions from code review
stevepiercy Jan 4, 2025
a8cbfb0
Apply suggestions from code review
erral Jan 5, 2025
3102b4d
signal that we do not need an edit component
erral Jan 5, 2025
ad7b2c0
show the field added by the schema enhancer in the corresponding vari…
erral Jan 5, 2025
59328e7
use the i18n pattern in the schema enhancer
erral Jan 5, 2025
83405f8
signal that the folder will be used to save the variations and the sc…
erral Jan 5, 2025
5244365
split comments to make them visible easier
erral Jan 5, 2025
8dbf8f8
rewrite the block configuration settings docs
erral Jan 5, 2025
43d0fa6
add an example with a custom edit form
erral Jan 5, 2025
720b3e2
rename the blocks
erral Jan 5, 2025
c06b755
Add add and edit content SVG icons
stevepiercy Jan 5, 2025
591505f
Apply suggestions from code review
stevepiercy Jan 5, 2025
f1abca2
Apply omitted suggestions from code review
stevepiercy Jan 5, 2025
058b4b0
Break comments into 2 lines to avoid overflow scrollbar when viewing …
ichim-david Jan 6, 2025
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: 1 addition & 1 deletion docs/source/blocks/anatomy.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ You can use all these props to render your edit block and model its behavior.
Volto later then 16.0.0 ships with a set of default Edit and View components.
The view component is mostly a placeholder, with an auto-generated listing of
the block fields, while the default Edit component is the most interesting, as
it can use the `schema` that you can specify in the block configuration to
it can use the `blockSchema` that you can specify in the block configuration to
automatically render a form for the Block settings, in the Volto Sidebar. In
the main editing area, it will render the view component, so for many blocks
you can just develop a schema and the View component.
Expand Down
74 changes: 47 additions & 27 deletions docs/source/blocks/editcomponent.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ myst:

The edit component part of a block anatomy is specially different to the view component because they have to support the UX for editing the block.
This UX can be very complex depending on the kind of block and the feature that it is trying to provide.
The project requirements will tell how far you should go with the UX story of each tile, and how complex it will become.
The project requirements will tell how far you should go with the UX story of each block, and how complex it will become.
You can use all the props that the edit component is receiving to model the UX for the block and how it will render.

See the complete list of {ref}`block-edit-component-props-label`.
Expand All @@ -28,22 +28,36 @@ You need to instantiate it this way:
```jsx
import { SidebarPortal } from '@plone/volto/components';

[...]
const Edit = (props) => {
const { selected } = props;
return (

[...]

<SidebarPortal selected={this.props.selected}>
// ...
</SidebarPortal>
<SidebarPortal selected={selected}>
// ...
</SidebarPortal>
)

}
```

Everything that's inside the `SidebarPortal` component will be rendered in the sidebar. If you need an extra layer of configuration within `SidebarPortal`, you can use `SidebarPopup`.

```jsx

import { SidebarPopup } from '@plone/volto/components';

<SidebarPopup open={this.props.sidebarOpen}>
...
</SidebarPopup>
const Edit = (props) => {
const { sidebarOpen } = props;

return (
[...]

<SidebarPopup open={sidebarOpen}>
...
</SidebarPopup>
)
}
```

## Schema driven automated block settings forms
Expand Down Expand Up @@ -107,24 +121,30 @@ import schema from './schema';
import BlockDataForm from '@plone/volto/components/manage/Form/BlockDataForm';
import { Icon } from '@plone/volto/components';

<SidebarPortal selected={this.props.selected}>
<BlockDataForm
icon={<Icon size="24px" name={nameSVG} />}
schema={schema}
title={schema.title}
headerActions={<button onClick={() => {}}>Action</button>}
footer={<div>I am footer</div>}
onChangeField={(id, value) => {
this.props.onChangeBlock(this.props.block, {
...this.props.data,
[id]: value,
});
}}
onChangeBlock={onChangeBlock}
formData={this.props.data}
block={block}
/>
</SidebarPortal>;
const Edit = (props) => {
const {selected, block, data, onChangeBlock} = props;

return (
<SidebarPortal selected={selected}>
<BlockDataForm
icon={<Icon size="24px" name={nameSVG} />}
schema={schema}
title={schema.title}
headerActions={<button onClick={() => {}}>Action</button>}
footer={<div>I am footer</div>}
onChangeField={(id, value) => {
onChangeBlock(block, {
...data,
[id]: value,
});
}}
onChangeBlock={onChangeBlock}
formData={data}
block={block}
/>
</SidebarPortal>;
)
}
```

## Object Browser
Expand Down
165 changes: 165 additions & 0 deletions docs/source/blocks/examples/custom-schema-and-view.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
---
myst:
html_meta:
"description": "Volto block with custom schema and view components"
"property=og:description": "Volto block with custom schema and view components"
"property=og:title": "Volto block with custom schema and view components"
"keywords": "Volto, React, blocks, grid, container, Plone"
---

(custom-schema-and-view)=

# Block with a custom schema

You can create a block with several settings defined using a schema, and let Volto render the edit form by itself.

To do so, define the schema, the view component, and configure the block settings.

## Preparations

In your Volto add-on, create a subfolder {file}`ExampleBlock02` inside the {file}`components` folder to save all the files required to create a block.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erral this is the first document that you have created why do we have example block 02? it makes more sense for me to start with ExampleBlock01 or simply have block1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrll, this comes from examples I have written for my team. There are 6 examples, but I have brought here only 3, the most relevant ones. I have copy-pasted the code, so that's why we have 2, 5 and 6.

I haven't copied the following ones:

  • Block just with schema (automatic view and edit)
  • Custom edit component (I will add this following your suggestion)
  • Block with 'manual' variations: adding the variation field manually in the schema


## Schema

Create a {file}`Schema.js` file inside the {file}`ExampleBlock02` folder, with the following contents.

```js
import messages from './messages';

const Schema = ({ intl }) => {
return {
title: intl.formatMessage(messages.block02),
block: 'block02',
fieldsets: [
{
id: 'default',
title: intl.formatMessage(messages.default),
fields: ['url', 'title'],
},
],

properties: {
url: {
title: intl.formatMessage(messages.URL),
widget: 'url',
},
title: {
title: intl.formatMessage(messages.title),
},
},
required: [],
};
};

export default Schema;
```

## Messages

As you may have noted, you prepared the block for internationalization.
{term}`Internationalization` (i18n) is the process of creating user interfaces which are suitable for different languages and cultural contexts.
To add translatable messages, create a file {file}`messages.js` in the same {file}`ExampleBlock02` folder with the following contents.

```js
import { defineMessages } from 'react-intl';

const messages = defineMessages({
block02: {
id: 'block02',
defaultMessage: 'Block 02',
},
default: {
id: 'default',
defaultMessage: 'Default',
},
URL: {
id: 'URL',
defaultMessage: 'URL',
},
title: {
id: 'title',
defaultMessage: 'Title',
},
});

export default messages;
```

## View component

The view component will have all the required logic to show the information saved on the block.
It will be a small HTML fragment.

Create a file {file}`View.jsx` in the {file}`ExampleBlock02` folder with the following contents.

```jsx
import cx from 'classnames';
import React from 'react';

const View = (props) => {
// `data` holds the values entered in the block edit form.
// `className` holds the CSS class names injected into this block by Volto's `styleClassNameExtenders`.
// `style` holds the CSS properties injected into this block by Volto's `Block Sytle Wrapper`.
const { data, className, style } = props;
return (
<div className={cx('block', 'block02', className)} style={style}>
I am the Block view component!
<ul>
<li>Title: {data.title}</li>
<li>URL: {data.url}</li>
</ul>
</div>
);
};

export default View;
```

## Block configuration

With all the block components ready, you need to register the block into Volto.

To do so, open your add-on's {file}`index.js` file, and insert the following contents.

```js
const applyConfig = (config) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erral I don't think this info should be here. It has nothing to do with the example given here. I would show the contents of the index file that is created whenever you create a new add-on.
From there, I would show that we can add our configuration for the block.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will rewrite this, saying to add the block configuration form before the return config, that will be easier to understand.

config.settings.isMultilingual = false;
config.settings.supportedLanguages = ['en'];
config.settings.defaultLanguage = 'en';


return config;
}

export default applyConfig;
```

Before the last `return config;` statement, write the following configuration.

```js
config.blocks.blocksConfig.block02 = {
id: 'block02', // this is the block id, it must match the id on the previous line
title: 'Block 02', // this is the block title
view: View02, // this is the block's view component
// edit: null;
blockSchema: Schema02, // this is the schema that will be used to render the edit form
icon: imagesSVG, // this is the image that will be shown in the block selector
sidebarTab: 1, // this is set to 1 to have the `Block` tab selected in the sidebar editor when editing this block
};
```

At the top of the file, import the relevant components as follows.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


```js
import View02 from './components/ExampleBlock02/View';
import Schema02 from './components/ExampleBlock02/Schema';

// This is the icon we use for the example, use a meaningful one or provide your own image.
import imagesSVG from '@plone/volto/icons/images.svg';
```

## See it in action

Your block is ready to be used in your site.

Restart your Volto site, and you can add it using the block add form.
Loading
Loading