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
…lapsed property
  • Loading branch information
alizedebray committed Jul 2, 2024
1 parent 10b906c commit a74d0fe
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 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 to toggle the visibility of the post-collapsible element 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 @@ -30,7 +30,16 @@ <h2>Post Card-Control</h2>

<div class="my-4">
<h2>Post Collapsible</h2>
<post-collapsible>
<button
(click)="isCollapsed = !isCollapsed"
[attr.aria-expanded]="!isCollapsed"
aria-controls="collapsible"
class="btn btn-default mb-regular"
type="button"
>
Toggle
</button>
<post-collapsible id="collapsible" [collapsed]="isCollapsed">
<p>Contentus momentus vero siteos et accusam iretea et justo.</p>
</post-collapsible>
</div>
Expand Down
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;
}
4 changes: 2 additions & 2 deletions packages/components/src/components.d.ts
Original file line number Diff line number Diff line change
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 @@ -600,7 +600,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 @@ -33,17 +33,19 @@ export class PostCollapsible {
@State() id: string;

/**
* 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;

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

void this.toggle(!collapsed);
}

/**
Expand All @@ -53,16 +55,12 @@ export class PostCollapsible {
*/
@Event() postToggle: EventEmitter<boolean>;

connectedCallback() {
this.validateCollapsed();
}

componentWillRender() {
this.id = this.host.id || `c${crypto.randomUUID()}`;
}

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

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 @@ -61,8 +62,25 @@ function externalControls(story: StoryFn, context: StoryContext) {
}

//RENDERER
let ignoreToggle = true;
function renderCollapsible(args: Partial<HTMLPostCollapsibleElement>) {
return html` <post-collapsible ${spreadArgs(args)}></post-collapsible> `;
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 ${spreadArgs(args)} @postToggle="${handleToggle}"></post-collapsible>
`;
}

// STORIES
Expand Down

0 comments on commit a74d0fe

Please sign in to comment.