-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): close button web component (#3880)
Co-authored-by: Oliver Schürch <[email protected]>
- Loading branch information
1 parent
5738e12
commit ae19d69
Showing
16 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@swisspost/design-system-documentation': minor | ||
'@swisspost/design-system-components': minor | ||
'@swisspost/design-system-styles': minor | ||
--- | ||
|
||
Added close button web component. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const CLOSE_BTN_ID = 'de313349-0c0b-4baf-adc6-cb8c2e36fc1a'; | ||
|
||
describe('Close button', () => { | ||
describe('default', () => { | ||
beforeEach(() => { | ||
cy.getComponent('post-closebutton', CLOSE_BTN_ID); | ||
}); | ||
|
||
it('should render with a close button and a11y label', () => { | ||
cy.get('@closebutton').should('exist'); | ||
cy.get('@closebutton').find('.btn-icon-close').should('exist'); | ||
cy.get('@closebutton').find('span.visually-hidden').should('exist'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Accessibility', () => { | ||
it('Has no detectable a11y violations on load for all variants', () => { | ||
cy.getSnapshots('post-closebutton'); | ||
cy.checkA11y('#root-inner'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/components/src/components/post-closebutton/post-closebutton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Component, Element, h, Host } from '@stencil/core'; | ||
import { version } from '@root/package.json'; | ||
|
||
/** | ||
* @slot default - Slot for placing visually hidden label in the close button. | ||
*/ | ||
@Component({ | ||
tag: 'post-closebutton', | ||
shadow: false, | ||
}) | ||
export class PostClosebutton { | ||
@Element() host: HTMLPostClosebuttonElement; | ||
|
||
render() { | ||
return ( | ||
<Host data-version={version}> | ||
<button class="btn btn-icon-close"> | ||
<post-icon aria-hidden="true" name="2043"></post-icon> | ||
<span class="visually-hidden"> | ||
<slot></slot> | ||
</span> | ||
</button> | ||
</Host> | ||
); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/components/src/components/post-closebutton/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# post-closebutton | ||
|
||
|
||
|
||
<!-- Auto Generated Below --> | ||
|
||
|
||
## Slots | ||
|
||
| Slot | Description | | ||
| ----------- | ----------------------------------------------------------- | | ||
| `"default"` | Slot for placing visually hidden label in the close button. | | ||
|
||
|
||
## Dependencies | ||
|
||
### Depends on | ||
|
||
- [post-icon](../post-icon) | ||
|
||
### Graph | ||
```mermaid | ||
graph TD; | ||
post-closebutton --> post-icon | ||
style post-closebutton fill:#f9f,stroke:#333,stroke-width:4px | ||
``` | ||
|
||
---------------------------------------------- | ||
|
||
*Built with [StencilJS](https://stenciljs.com/)* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/documentation/cypress/snapshots/components/close-button.snapshot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
describe('Close button', () => { | ||
it('post-closebutton', () => { | ||
cy.visit('/iframe.html?id=snapshots--post-closebutton'); | ||
cy.get('post-closebutton[data-hydrated]', { timeout: 30000 }).should('be.visible'); | ||
cy.percySnapshot('Close button (Web Component)', { widths: [1440] }); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...documentation/src/stories/components/buttons/close-button/close-button.docs.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Canvas, Controls, Meta, Source } from '@storybook/blocks'; | ||
import * as CloseButtonStories from './close-button.stories'; | ||
|
||
<Meta of={CloseButtonStories} /> | ||
|
||
<div className="docs-title"> | ||
# Close button | ||
|
||
<nav> | ||
<link-design of={JSON.stringify(CloseButtonStories)}></link-design> | ||
</nav> | ||
</div> | ||
|
||
<div className="lead"> | ||
The close button is a web component. It is a slightly adapted version of the icon button, intended | ||
for a specific use case. | ||
</div> | ||
|
||
The value inside the `<post-closebutton>` element will be rendered inside the button as a visually hidden text so make sure to only insert inline content there. | ||
|
||
<Canvas sourceState="shown" of={CloseButtonStories.Default} /> | ||
<div className="hide-col-default"> | ||
<Controls of={CloseButtonStories.Default} /> | ||
</div> |
26 changes: 26 additions & 0 deletions
26
...ocumentation/src/stories/components/buttons/close-button/close-button.snapshot.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Args, StoryContext, StoryObj } from '@storybook/web-components'; | ||
import meta, { Default } from './close-button.stories'; | ||
import { html } from 'lit'; | ||
|
||
const { id, ...metaWithoutId } = meta; | ||
|
||
export default { | ||
...metaWithoutId, | ||
title: 'Snapshots', | ||
}; | ||
|
||
type Story = StoryObj<HTMLPostClosebuttonElement>; | ||
|
||
export const PostClosebutton: Story = { | ||
render: (_args: Args, context: StoryContext<HTMLPostClosebuttonElement>) => { | ||
return html` | ||
${['bg-white', 'bg-dark'].map( | ||
bg => html` | ||
<div class="${bg} p-40" data-color-scheme=${bg === 'bg-white' ? 'light' : 'dark'}> | ||
${Default.render?.({ ...context.args }, context)} | ||
</div> | ||
`, | ||
)} | ||
`; | ||
}, | ||
}; |
39 changes: 39 additions & 0 deletions
39
packages/documentation/src/stories/components/buttons/close-button/close-button.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Args, StoryObj } from '@storybook/web-components'; | ||
import { html } from 'lit/static-html.js'; | ||
import { MetaComponent } from '@root/types'; | ||
import { unsafeHTML } from 'lit/directives/unsafe-html.js'; | ||
|
||
const meta: MetaComponent = { | ||
id: 'de313349-0c0b-4baf-adc6-cb8c2e36fc1a', | ||
title: 'Components/Buttons/Close button', | ||
component: 'post-closebutton', | ||
tags: ['package:WebComponents'], | ||
parameters: { | ||
badges: [], | ||
design: { | ||
type: 'figma', | ||
url: 'https://www.figma.com/design/JIT5AdGYqv6bDRpfBPV8XR/Foundations-%26-Components-Next-Level?node-id=2514-18516&t=gCGlckfBEobCTna3-4', | ||
}, | ||
}, | ||
args: { | ||
'slots-default': 'Close button', | ||
}, | ||
argTypes: { | ||
'slots-default': { | ||
name: 'Label', | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj; | ||
|
||
export const Default: Story = { | ||
render: (args: Args) => { | ||
return html`<post-closebutton>${unsafeHTML(args['slots-default'])}</post-closebutton> `; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@use '../functions/tokens'; | ||
@use '../tokens/components'; | ||
@use './../mixins/utilities'; | ||
|
||
tokens.$default-map: components.$post-close; | ||
|
||
.btn-icon-close { | ||
padding: 0; | ||
border: unset; | ||
min-height: unset; | ||
min-width: unset; | ||
width: tokens.get('close-size'); | ||
height: tokens.get('close-size'); | ||
border-radius: tokens.get('close-border-radius'); | ||
background-color: tokens.get('close-enabled-bg'); | ||
color: tokens.get('close-enabled-fg'); | ||
|
||
> post-icon { | ||
width: tokens.get('close-icon-size'); | ||
height: tokens.get('close-icon-size'); | ||
} | ||
|
||
@include utilities.not-disabled-hover() { | ||
cursor: pointer; | ||
background-color: tokens.get('close-hover-bg'); | ||
color: tokens.get('close-hover-fg'); | ||
} | ||
} |