Skip to content

Commit

Permalink
feat(components, components-angular): update the post-collapsible col… (
Browse files Browse the repository at this point in the history
#3205)

…lapsed property

---------

Co-authored-by: Philipp Gfeller <[email protected]>
  • Loading branch information
alizedebray and gfellerph committed Jul 23, 2024
1 parent 0ef74a4 commit ed75a29
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .changeset/plenty-experts-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@swisspost/design-system-documentation': patch
'@swisspost/design-system-components': patch
'@swisspost/design-system-components-angular': patch
---

Updated the `collapsed` property of the `post-collapsible` and `post-accordion-item` to toggle the content visibility throughout the component lifecycle, rather than only initially.
5 changes: 5 additions & 0 deletions .changeset/thin-eyes-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-components-angular-workspace': patch
---

Added a toggle button for the `post-collapsible`.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import { Component } from '@angular/core';
selector: 'home-page',
templateUrl: './home.component.html',
})
export class HomeComponent {}
export class HomeComponent {
isCollapsed = false;
}
8 changes: 4 additions & 4 deletions packages/components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export namespace Components {
}
interface PostAccordionItem {
/**
* If `true`, the element is initially collapsed otherwise it is displayed.
* If `true`, the element is collapsed otherwise it is displayed.
*/
"collapsed"?: boolean;
/**
Expand Down Expand Up @@ -126,7 +126,7 @@ export namespace Components {
}
interface PostCollapsible {
/**
* If `true`, the element is initially collapsed otherwise it is displayed.
* If `true`, the element is collapsed otherwise it is displayed.
*/
"collapsed"?: boolean;
/**
Expand Down Expand Up @@ -535,7 +535,7 @@ declare namespace LocalJSX {
}
interface PostAccordionItem {
/**
* If `true`, the element is initially collapsed otherwise it is displayed.
* If `true`, the element is collapsed otherwise it is displayed.
*/
"collapsed"?: boolean;
/**
Expand Down Expand Up @@ -621,7 +621,7 @@ declare namespace LocalJSX {
}
interface PostCollapsible {
/**
* If `true`, the element is initially collapsed otherwise it is displayed.
* If `true`, the element is collapsed otherwise it is displayed.
*/
"collapsed"?: boolean;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ export class PostAccordionItem {
@Element() host: HTMLPostAccordionItemElement;

@State() id: string;
@State() isOpen: boolean;

/**
* If `true`, the element is initially collapsed otherwise it is displayed.
* If `true`, the element is collapsed otherwise it is displayed.
*/
@Prop() readonly collapsed?: boolean = false;
@Prop({ mutable: true }) collapsed?: boolean = false;

/**
* Defines the hierarchical level of the accordion item header within the headings structure.
Expand All @@ -46,14 +45,14 @@ export class PostAccordionItem {
}

componentWillLoad() {
this.isOpen = !this.collapsed;
this.id = this.host.id || `a${crypto.randomUUID()}`;
}

@Listen('postToggle')
// capture to make sure the "collapsed" property is updated before the event is consumed
@Listen('postToggle', { capture: true })
onCollapseToggle(event: CustomEvent<boolean>): void {
if ((event.target as HTMLElement).localName === 'post-accordion-item') {
this.isOpen = event.detail;
this.collapsed = !event.detail;
}
}

Expand All @@ -74,8 +73,8 @@ export class PostAccordionItem {
<HeadingTag class="accordion-header" id={`${this.id}--header`}>
<button
aria-controls={`${this.id}--collapse`}
aria-expanded={`${this.isOpen}`}
class={`accordion-button${this.isOpen ? '' : ' collapsed'}`}
aria-expanded={`${!this.collapsed}`}
class={`accordion-button${this.collapsed ? ' collapsed' : ''}`}
onClick={() => this.toggle()}
type="button"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

| Property | Attribute | Description | Type | Default |
| -------------- | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | ------- |
| `collapsed` | `collapsed` | If `true`, the element is initially collapsed otherwise it is displayed. | `boolean` | `false` |
| `collapsed` | `collapsed` | If `true`, the element is collapsed otherwise it is displayed. | `boolean` | `false` |
| `headingLevel` | `heading-level` | <span style="color:red">**[DEPRECATED]**</span> set the `heading-level` property on the parent `post-accordion` instead.<br/><br/>Defines the hierarchical level of the accordion item header within the headings structure. | `1 \| 2 \| 3 \| 4 \| 5 \| 6` | `2` |


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ export class PostCollapsible {
@Element() host: HTMLPostCollapsibleElement;

/**
* If `true`, the element is initially collapsed otherwise it is displayed.
* If `true`, the element is collapsed otherwise it is displayed.
*/
@Prop() readonly collapsed?: boolean = false;
@Prop({ mutable: true }) collapsed?: boolean = false;

@Watch('collapsed')
validateCollapsed(newValue = this.collapsed) {
collapsedChange() {
checkEmptyOrType(
newValue,
this.collapsed,
'boolean',
'The `collapsed` property of the `post-collapsible` must be a boolean.',
);

void this.toggle(!this.collapsed);
}

/**
Expand All @@ -49,12 +51,8 @@ export class PostCollapsible {
*/
@Event() postToggle: EventEmitter<boolean>;

connectedCallback() {
this.validateCollapsed();
}

componentDidLoad() {
if (this.collapsed) void this.toggle(false);
this.collapsedChange();
this.isLoaded = true;

this.updateTriggers();
Expand All @@ -69,8 +67,9 @@ export class PostCollapsible {
async toggle(open = !this.isOpen): Promise<boolean> {
if (open === this.isOpen) return open;

this.isOpen = !this.isOpen;
if (this.isLoaded) this.postToggle.emit(this.isOpen);
this.isOpen = open;
this.collapsed = !open;
if (this.isLoaded) this.postToggle.emit(open);

const animation = open ? expand(this.host) : collapse(this.host);

Expand All @@ -80,7 +79,7 @@ export class PostCollapsible {

animation.commitStyles();

return this.isOpen;
return open;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/components/post-collapsible/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

## Properties

| Property | Attribute | Description | Type | Default |
| ----------- | ----------- | ------------------------------------------------------------------------ | --------- | ------- |
| `collapsed` | `collapsed` | If `true`, the element is initially collapsed otherwise it is displayed. | `boolean` | `false` |
| Property | Attribute | Description | Type | Default |
| ----------- | ----------- | -------------------------------------------------------------- | --------- | ------- |
| `collapsed` | `collapsed` | If `true`, the element is collapsed otherwise it is displayed. | `boolean` | `false` |


## Events
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useArgs } from '@storybook/preview-api';
import { StoryContext, StoryFn, StoryObj } from '@storybook/web-components';
import { html } from 'lit';
import { spreadArgs } from '@/utils';
Expand Down Expand Up @@ -31,16 +32,30 @@ function gap(story: StoryFn, context: StoryContext) {
}

//RENDERER
let ignoreToggle = true;
function renderCollapsible(
{ innerHTML, ...args }: Partial<HTMLPostCollapsibleElement>,
context: StoryContext<HTMLPostCollapsibleElement>,
) {
const [_, updateArgs] = useArgs();
const handleToggle = (e: CustomEvent<boolean>) => {
if (ignoreToggle) return;

const collapsed = !e.detail;
updateArgs({ collapsed });
};

// ignore the first toggle event after a collapsed arg update
ignoreToggle = true;
setTimeout(() => {
ignoreToggle = false;
}, 200);
return html`
<post-collapsible-trigger for=${context.id}>
<button class="btn btn-secondary">Toggle Collapsible</button>
</post-collapsible-trigger>
<post-collapsible id=${context.id} ${spreadArgs(args)}>
<post-collapsible id=${context.id} ${spreadArgs(args)} @postToggle="${handleToggle}">
${unsafeHTML(innerHTML)}
</post-collapsible>
`;
Expand Down

0 comments on commit ed75a29

Please sign in to comment.