Skip to content

Commit

Permalink
docs(ui5-toolbar): fix documentation (#7667)
Browse files Browse the repository at this point in the history
fix(ui5-toolbar): documentation

Co-authored-by: PetyaMarkovaBogdanova <[email protected]>
  • Loading branch information
PetyaMarkovaBogdanova and PetyaMarkovaBogdanova authored Oct 30, 2023
1 parent 1941854 commit 54a411d
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 3 deletions.
146 changes: 146 additions & 0 deletions docs/internal/Toolbar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Creating a web component abstract item to be used inside Toolbar

*This section explains how to build abstract items in order to be compatible with UI5 Toolbar.*
*It will guide you through the process of how we created `ui5-toolbar-button`, to be
compatible with `ui5-toolbar`. Currently developed items can be used without those efforts. They are:*
1. ui5-toolbar-button
2. ui5-toolbar-select
3. ui5-toolbar-separator
4. ui5-toolbar-spacer

## Abstract items

### Why are abstract items needed?

When the toolbar renders its slotted items within a popover in the static area, simply relocating the actual DOM nodes within its slots can lead to reference issues, causing the slotted nodes to lose their parent reference (e.g., the toolbar). This is the reason why the toolbar must operate with abstract items. Abstract items are not rendered directly within the DOM; instead, they function as data used by the toolbar to produce corresponding physical web components. On the other hand, useful modifications detected by the toolbar on the physical items are synchronised with the abstract ones. (see step [Events](#events))

The `ui5-toolbar` is a composite web component, that slots different UI5 components, designing them as abstract items. They can contain
properties, slots and events, and they can match the API of already existing component.
In order to be suitable for usage inside `ui5-toolbar`, each component should adhere to following guidelines:


1. The component needs to implement a class with component name of the following type:

```javascript
ToolbarButton.ts
```

2. The new component needs to implement two template files with name of the following type:

```javascript
ToolbarButton.hbs and ToolbarPopoverButton.hbs
```

3. It needs to implement **customElement** decorator, which is good to contain custom tag name:

```javascript
@customElement({
tag: "ui5-toolbar-button"
})
```

4. The class should extend **ToolbarItem** base class, which should also be added as a dependency.

```javascript
class ToolbarButton extends ToolbarItem
```

5. Inside the module there should be two template getters: for toolbar and popover representation.

```javascript
static get toolbarTemplate() {
return ToolbarButtonTemplate;
}

static get toolbarPopoverTemplate() {
return ToolbarPopoverButtonTemplate;
}
```

6. After the class declaration there should be a registry call for the item inside the toolbar. **registerToolbarItem** helper should be added as a dependency.

```javascript
import { registerToolbarItem } from "./ToolbarRegistry.js";
```

```javascript
registerToolbarItem(ToolbarButton);
```

7. In the templates there should be mapping of the properties that need to be used in the component inside Toolbar.

Inside ToolbarButton.ts:

```typescript
@property()
text!: string;

@property({ type: Boolean })
disabled!: boolean;
```

Inside ToolbarButtonTemplate.hbs:

```html
<ui5-button
  class="ui5-tb-item"
  ?disabled="{{this.disabled}}"
  data-ui5-external-action-item-id="{{this._id}}"
  data-ui5-stable="{{this.stableDomRef}}"
>
  {{this.text}}
</ui5-button>
```
8. The new component's DOM root element needs to have `"ui5-tb-item"` CSS class in order to get default styles for item (margins etc.).
9. The new class needs to be added to the bundle file in the corresponding library.

Inside bundle.common.js:
```javascript
import ToolbarButton from "./dist/ToolbarButton.js";
```
10. Use your newly created component inside the ui5-toolbar like this:

```html
<ui5-toolbar>
  <ui5-toolbar-button text="Button 1" disabled></ui5-toolbar-button>
  <ui5-toolbar-button text="Button 2"></ui5-toolbar-button>
</ui5-toolbar>
```

## Events

Abstract items can provide a map of events through the `subscribedEvents` getter. The toolbar will actively monitor these events on the physical items, and when triggered, it will also fire the information to the corresponding abstract item. This mechanism proves useful when the abstract item requires synchronization of changes or interactions with the physical items. Importantly, events described as public offer benefits to consumers of the abstract items informing them about interactions with the physical elements. Additionally, the map contains information about the popover, such as `preventClosing: true`, which ensures that the popover remains open when this event is triggered by the physical item.

A good example is the Map of the `ui5-toolbar-select`:

```javascript
get subscribedEvents() {
  const map = new Map();

  map.set("click", { preventClosing: true });
  map.set("change", { preventClosing: false });
  map.set("open", { preventClosing: true });
  map.set("close", { preventClosing: true });

  return map;
}
```

The `ui5-toolbar-select` then waits for the toolbar to fire the `change` event, in order to notify (synchronize) its `options` slots:

```ts
_onEventHandler(e: Event): void {
  if (e.type === "change") {
    // update options
    const selectedOption = (e as CustomEvent<ToolbarSelectChangeEventDetail>).detail.selectedOption;
    const selectedOptionIndex = Number(selectedOption?.getAttribute("data-ui5-external-action-item-index"));
    this.options.forEach((option: Option, index: number) => {
      if (index === selectedOptionIndex) {
        option.setAttribute("selected", "");
      } else {
        option.removeAttribute("selected");
      }
    });
  }
}
```
3 changes: 3 additions & 0 deletions packages/main/src/themes/ToolbarPopover.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

.ui5-tb-popover-item {
width: 100%;
}

.ui5-tb-popover-item:not(:last-child) {
margin-bottom: 0.25rem;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/main/test/pages/Toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
});

select.addEventListener("ui5-change", e => {
console.log("Selected " + e.detail.selectedOption.getAttribute("text"));
console.log("Selected " + e.detail.selectedOption.textContent);
});

select.addEventListener("ui5-open", e => {
Expand Down
98 changes: 97 additions & 1 deletion packages/playground/_stories/main/Toolbar/Toolbar.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ const Template: UI5StoryArgs<Toolbar, StoryArgsSlots> = (args) => {
align-content="${ifDefined(args.alignContent)}"
>
${unsafeHTML(args.default)}
</ui5-toolbar>`;
</ui5-toolbar>
<script>
select.addEventListener("ui5-change", e => {
textarea.setAttribute("value", "Selected option is:" + e.detail.selectedOption.textContent);
});
</script>
`;
};

export const Basic = Template.bind({});
Expand Down Expand Up @@ -66,3 +72,93 @@ Basic.args = {

export const Types: StoryFn = TemplateToolbarTypes.bind({});

export const MultipleUI5SelectComponents : UI5StoryArgs<Toolbar, StoryArgsSlots> = (args) => {
return html`<ui5-toolbar
align-content="${ifDefined(args.alignContent)}"
>
${unsafeHTML(args.default)}
</ui5-toolbar>
<ui5-textarea disabled id="ToolbarStoryTextarea" placeholder="Change selection of the first Select Box"></ui5-textarea>
<script>
ToolbarStorySelect.addEventListener("ui5-change", e => {
ToolbarStoryTextarea.setAttribute("value", "Selected option is: " + e.detail.selectedOption.textContent);
});
</script>
`;
};


MultipleUI5SelectComponents.storyName = "Multiple Toolbar Select components";

MultipleUI5SelectComponents.args = {
default: `<ui5-toolbar-select id="ToolbarStorySelect">
<ui5-toolbar-select-option>Apple</ui5-toolbar-select-option>
<ui5-toolbar-select-option>Orange</ui5-toolbar-select-option>
<ui5-toolbar-select-option>Pear</ui5-toolbar-select-option>
</ui5-toolbar-select>
<ui5-toolbar-select>
<ui5-toolbar-select-option>1</ui5-toolbar-select-option>
<ui5-toolbar-select-option>2</ui5-toolbar-select-option>
<ui5-toolbar-select-option>3</ui5-toolbar-select-option>
</ui5-toolbar-select>
<ui5-toolbar-select>
<ui5-toolbar-select-option>Bulgaria</ui5-toolbar-select-option>
<ui5-toolbar-select-option>Bolivia</ui5-toolbar-select-option>
<ui5-toolbar-select-option>Brunei</ui5-toolbar-select-option>
<ui5-toolbar-select-option>Bangladesh</ui5-toolbar-select-option>
<ui5-toolbar-select-option>Belarus</ui5-toolbar-select-option>
<ui5-toolbar-select-option>Belgium</ui5-toolbar-select-option>
</ui5-toolbar-select>
<ui5-toolbar-select value-state="Success" width="auto">
<ui5-toolbar-select-option icon="meal" selected="">Apple</ui5-toolbar-select-option>
<ui5-toolbar-select-option icon="meal">Avocado</ui5-toolbar-select-option>
<ui5-toolbar-select-option icon="meal">Mango</ui5-toolbar-select-option>
</ui5-toolbar-select>
<ui5-toolbar-select value-state="Warning" width="auto">
<ui5-toolbar-select-option icon="meal">Orange</ui5-toolbar-select-option>
<ui5-toolbar-select-option icon="meal" selected="">Pumpkin</ui5-toolbar-select-option>
<ui5-toolbar-select-option icon="meal">Carrot</ui5-toolbar-select-option>
<div slot="valueStateMessage">
Information message. This is a <a href="#">Link</a>. Extra long text
used as an information message. Extra long text used as an information
message - 2. Extra long text used as an information message - 3.
</div>
<div slot="valueStateMessage">
Information message 2. This is a <a href="#">Link</a>. Extra long text
used as an information message. Extra long text used as an information
message - 2. Extra long text used as an information message - 3.
</div>
</ui5-toolbar-select>
<ui5-toolbar-select value-state="Error">
<ui5-toolbar-select-option icon="meal">Strawberry</ui5-toolbar-select-option>
<ui5-toolbar-select-option icon="meal">Tomato</ui5-toolbar-select-option>
<ui5-toolbar-select-option icon="meal" selected="">Red Chili Pepper</ui5-toolbar-select-option>
<div slot="valueStateMessage">
Information message. This is a <a href="#">Link</a>. Extra long text
used as an information message. Extra long text used as an information
message - 2. Extra long text used as an information message - 3.
</div>
<div slot="valueStateMessage">
Information message 2. This is a <a href="#">Link</a>. Extra long text
used as an information message. Extra long text used as an information
message - 2. Extra long text used as an information message - 3.
</div>
</ui5-toolbar-select>
<ui5-toolbar-select value-state="Information">
<ui5-toolbar-select-option icon="meal">Blueberry</ui5-toolbar-select-option>
<ui5-toolbar-select-option icon="meal">Grape</ui5-toolbar-select-option>
<ui5-toolbar-select-option icon="meal" selected="">Plum</ui5-toolbar-select-option>
<div slot="valueStateMessage">
Information message. This is a <a href="#">Link</a>. Extra long text
used as an information message. Extra long text used as an information
message - 2. Extra long text used as an information message - 3.
</div>
<div slot="valueStateMessage">
Information message 2. This is a <a href="#">Link</a>. Extra long text
used as an information message. Extra long text used as an information
message - 2. Extra long text used as an information message - 3.
</div>
</ui5-toolbar-select>
`
};

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const docsGenerator = new DocsGenerator({
reader: new DocsReader({
directoryUtils,
src: directoryUtils.joinPath("../../../docs/**/*.md"),
ignore: ["/**/*/README.md", "/**/*/images", "/**/*/changelog"],
ignore: ["/**/*/README.md", "/**/*/images", "/**/*/changelog", "/**/*/internal"],
}),
parsers: [
new MetadataParser(),
Expand Down

0 comments on commit 54a411d

Please sign in to comment.