Skip to content

Commit

Permalink
chore(components): update crypto to nanoid (#4369)
Browse files Browse the repository at this point in the history
Co-authored-by: Alizé Debray <[email protected]>
  • Loading branch information
leagrdv and alizedebray authored Jan 8, 2025
1 parent bf95c9b commit ea5c638
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-grapes-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-components': patch
---

Reduced the length of random IDs generated in the components; they are now generated using the [nanoid library](https://github.com/ai/nanoid) instead of the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API).
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, Element, h, Host, Listen, Method, Prop, State, Watch } from
import { version } from '@root/package.json';
import { HEADING_LEVELS, HeadingLevel } from '@/types';
import { checkEmptyOrOneOf } from '@/utils';
import { nanoid } from 'nanoid';

/**
* @part button - The pseudo-element, used to override styles on the components internal header `button` element.
Expand Down Expand Up @@ -46,7 +47,7 @@ export class PostAccordionItem {
}

componentWillLoad() {
this.id = this.host.id || `a${crypto.randomUUID()}`;
this.id = this.host.id || `p${nanoid(6)}`;
}

componentDidLoad() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { version } from '@root/package.json';
import { fadeOut } from '@/animations';
import { checkEmptyOrOneOf, checkEmptyOrPattern, checkNonEmpty, checkType } from '@/utils';
import { BANNER_TYPES, BannerType } from './banner-types';
import { nanoid } from 'nanoid';

/**
* @slot heading - Slot for placing custom content within the banner's heading.
Expand All @@ -29,7 +30,7 @@ import { BANNER_TYPES, BannerType } from './banner-types';
export class PostBanner {
@Element() host: HTMLPostBannerElement;

@State() bannerId = crypto.randomUUID();
@State() bannerId = `p${nanoid(6)}`;
@State() classes: string;
@State() hasActions: boolean;
@State() hasHeading: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/components/post-list/post-list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, Element, Prop, Host, State, h } from '@stencil/core';
import { version } from '@root/package.json';
import { nanoid } from 'nanoid';

/**
* @slot default - Slot for placing the list title.
Expand Down Expand Up @@ -35,7 +36,7 @@ export class PostList {
/**
* Get the id set on the host element or use a random id by default
*/
this.titleId = `title-${this.host.id || crypto.randomUUID()}`;
this.titleId = `title-${this.host.id || nanoid(6)}`;
}

componentDidLoad() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, Element, h, Host, Prop, State, Watch } from '@stencil/core';
import { version } from '@root/package.json';
import { checkNonEmpty } from '@/utils';
import { nanoid } from 'nanoid';

/**
* @slot default - Slot for the content of the tab header.
Expand All @@ -27,7 +28,7 @@ export class PostTabHeader {
}

componentWillLoad() {
this.tabId = `tab-${this.host.id || crypto.randomUUID()}`;
this.tabId = `tab-${this.host.id || nanoid(6)}`;
}

render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, Element, h, Host, Prop, State } from '@stencil/core';
import { version } from '@root/package.json';
import { nanoid } from 'nanoid';

/**
* @slot default - Slot for placing the content of the tab panel.
Expand All @@ -22,7 +23,7 @@ export class PostTabPanel {

componentWillLoad() {
// get the id set on the host element or use a random id by default
this.panelId = `panel-${this.host.id || crypto.randomUUID()}`;
this.panelId = `panel-${this.host.id || nanoid(6)}`;
}

render() {
Expand Down

0 comments on commit ea5c638

Please sign in to comment.