-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
b6a6e7a
commit d8b3c9c
Showing
12 changed files
with
409 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,6 @@ | ||
--- | ||
'@swisspost/design-system-documentation': minor | ||
'@swisspost/design-system-components': minor | ||
--- | ||
|
||
Added the `post-togglebutton` 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,114 @@ | ||
const TOGGLE_BUTTON_ID = '1a6f47c2-5e8a-45a0-b1c3-9f7e2b834c24'; | ||
|
||
describe('togglebutton', () => { | ||
beforeEach(() => { | ||
cy.visit('/iframe.html?id=snapshots--toggle-button'); | ||
cy.get('post-togglebutton', { timeout: 30000 }).should('be.visible'); | ||
}); | ||
|
||
describe('default behavior', () => { | ||
it('should toggle state when clicked', () => { | ||
cy.get('post-togglebutton') | ||
.first() | ||
.as('button') | ||
.shadow() | ||
.find('slot[name="untoggled"]') | ||
.should('exist'); | ||
|
||
cy.get('@button').click(); | ||
|
||
cy.get('@button').shadow().find('slot[name="toggled"]').should('exist'); | ||
}); | ||
|
||
it('should toggle state when pressing Enter key', () => { | ||
cy.get('post-togglebutton') | ||
.first() | ||
.as('button') | ||
.shadow() | ||
.find('slot[name="untoggled"]') | ||
.should('exist'); | ||
|
||
cy.get('@button').trigger('keydown', { key: 'Enter' }); | ||
|
||
cy.get('@button').shadow().find('slot[name="toggled"]').should('exist'); | ||
}); | ||
|
||
it('should have correct ARIA attributes', () => { | ||
cy.get('post-togglebutton') | ||
.first() | ||
.as('button') | ||
.should('have.attr', 'role', 'button') | ||
.and('have.attr', 'aria-pressed', 'false') | ||
.and('have.attr', 'tabindex', '0'); | ||
|
||
cy.get('@button').click(); | ||
|
||
cy.get('@button').should('have.attr', 'aria-pressed', 'true'); | ||
}); | ||
}); | ||
|
||
describe('initial state', () => { | ||
it('should respect initial toggled state', () => { | ||
cy.get('post-togglebutton[toggled]') | ||
.first() | ||
.as('toggledButton') | ||
.shadow() | ||
.find('slot[name="toggled"]') | ||
.should('exist'); | ||
|
||
cy.get('@toggledButton').should('have.attr', 'aria-pressed', 'true'); | ||
}); | ||
|
||
it('should respect untoggled state', () => { | ||
cy.get('post-togglebutton:not([toggled])') | ||
.first() | ||
.as('untoggledButton') | ||
.shadow() | ||
.find('slot[name="untoggled"]') | ||
.should('exist'); | ||
|
||
cy.get('@untoggledButton').should('have.attr', 'aria-pressed', 'false'); | ||
}); | ||
}); | ||
|
||
describe('slot content', () => { | ||
it('should display correct slot content based on toggle state', () => { | ||
cy.get('post-togglebutton').first().as('button'); | ||
|
||
cy.get('@button').shadow().find('slot[name="untoggled"]').should('exist'); | ||
|
||
cy.get('@button').click(); | ||
|
||
cy.get('@button').shadow().find('slot[name="toggled"]').should('exist'); | ||
|
||
cy.get('@button').click(); | ||
|
||
cy.get('@button').shadow().find('slot[name="untoggled"]').should('exist'); | ||
}); | ||
}); | ||
|
||
describe('version attribute', () => { | ||
it('should have the correct version data attribute', () => { | ||
cy.get('post-togglebutton').first().should('have.attr', 'data-version'); | ||
}); | ||
}); | ||
|
||
describe('Accessibility', () => { | ||
beforeEach(() => { | ||
cy.injectAxe(); | ||
}); | ||
|
||
it('Has no detectable a11y violations on load for all variants', () => { | ||
cy.checkA11y('#root-inner'); | ||
}); | ||
|
||
it('Should be keyboard navigable', () => { | ||
cy.get('post-togglebutton') | ||
.first() | ||
.focus() | ||
.should('have.focus') | ||
.trigger('keydown', { key: 'Enter' }) | ||
.should('have.attr', 'aria-pressed', 'true'); | ||
}); | ||
}); | ||
}); |
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
3 changes: 3 additions & 0 deletions
3
packages/components/src/components/post-togglebutton/post-togglebutton.scss
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,3 @@ | ||
:host { | ||
cursor: pointer; | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/components/src/components/post-togglebutton/post-togglebutton.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,50 @@ | ||
import { Component, Host, h, Prop } from '@stencil/core'; | ||
import { version } from '@root/package.json'; | ||
|
||
/** | ||
* @slot toggled - Slot for content displayed when the button is in the "on" state. | ||
* @slot untoggled - Slot for content displayed when the button is in the "off" state. | ||
*/ | ||
|
||
@Component({ | ||
tag: 'post-togglebutton', | ||
styleUrl: 'post-togglebutton.scss', | ||
shadow: true, | ||
}) | ||
export class PostTogglebutton { | ||
/** | ||
* If `true`, the button is in the "on" state, otherwise it is in the "off" state. | ||
*/ | ||
@Prop({ reflect: true, mutable: true }) toggled: boolean = false; | ||
|
||
private handleClick = () => { | ||
this.toggled = !this.toggled; | ||
}; | ||
|
||
private handleKeydown = (event: KeyboardEvent) => { | ||
if (event.key === 'Enter') { | ||
this.toggled = !this.toggled; | ||
} | ||
}; | ||
|
||
render() { | ||
return ( | ||
<Host | ||
slot="post-togglebutton" | ||
tabindex="0" | ||
data-version={version} | ||
role="button" | ||
aria-pressed={this.toggled.toString()} | ||
onClick={this.handleClick} | ||
onKeyDown={this.handleKeydown} | ||
> | ||
<span hidden={this.toggled}> | ||
<slot name="untoggled" /> | ||
</span> | ||
<span hidden={!this.toggled}> | ||
<slot name="toggled" /> | ||
</span> | ||
</Host> | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/components/src/components/post-togglebutton/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,25 @@ | ||
# post-togglebutton | ||
|
||
|
||
|
||
<!-- Auto Generated Below --> | ||
|
||
|
||
## Properties | ||
|
||
| Property | Attribute | Description | Type | Default | | ||
| --------- | --------- | ------------------------------------------------------------------------------- | --------- | ------- | | ||
| `toggled` | `toggled` | If `true`, the button is in the "on" state, otherwise it is in the "off" state. | `boolean` | `false` | | ||
|
||
|
||
## Slots | ||
|
||
| Slot | Description | | ||
| ------------- | ----------------------------------------------------------------- | | ||
| `"toggled"` | Slot for content displayed when the button is in the "on" state. | | ||
| `"untoggled"` | Slot for content displayed when the button is in the "off" state. | | ||
|
||
|
||
---------------------------------------------- | ||
|
||
*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/toggle-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('Toggle Button', () => { | ||
it('default', () => { | ||
cy.visit('/iframe.html?id=snapshots--toggle-button'); | ||
cy.get('post-togglebutton[data-hydrated]', { timeout: 30000 }).should('be.visible'); | ||
cy.percySnapshot('Toggle Buttons', { widths: [320, 600, 1024] }); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/documentation/src/stories/components/togglebutton/togglebutton.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,31 @@ | ||
import { Canvas, Controls, Meta } from '@storybook/blocks'; | ||
import * as toggleButtonStories from './togglebutton.stories'; | ||
|
||
<Meta of={toggleButtonStories} /> | ||
|
||
# Toggle Button | ||
|
||
<p className="lead">A toggle button component to switch between two states and display different content based on the current state.</p> | ||
|
||
<Canvas sourceState="shown" of={toggleButtonStories.Default} /> | ||
<div className="hide-col-default"> | ||
<Controls of={toggleButtonStories.Default} /> | ||
</div> | ||
|
||
## Installation | ||
|
||
The `<post-togglebutton>` element is part of the `@swisspost/design-system-components` package. For more information, read the [getting started with components guide](/?path=/docs/edfb619b-fda1-4570-bf25-20830303d483--docs). | ||
|
||
## Examples | ||
|
||
### Initial Toggled State | ||
|
||
You can set the button to be toggled initially by using the `toggled` attribute. When set, the component will start in the toggled state and display the corresponding content. | ||
|
||
<Canvas of={toggleButtonStories.InitiallyToggled} /> | ||
|
||
### Icon content | ||
|
||
You can also add an icon to the content (for a toggle menu, for example). | ||
|
||
<Canvas of={toggleButtonStories.IconContent} /> |
46 changes: 46 additions & 0 deletions
46
packages/documentation/src/stories/components/togglebutton/togglebutton.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,46 @@ | ||
import type { StoryObj } from '@storybook/web-components'; | ||
import meta from './togglebutton.stories'; | ||
import { html } from 'lit'; | ||
|
||
const { id, ...metaWithoutId } = meta; | ||
|
||
export default { | ||
...metaWithoutId, | ||
title: 'Snapshots', | ||
}; | ||
|
||
const SCHEME = ['light', 'dark']; | ||
const BTN = ['btn-primary', 'btn-secondary', 'btn-terciary']; | ||
const SIZES = ['', 'btn-sm', 'btn-lg']; | ||
|
||
type Story = StoryObj; | ||
|
||
export const ToggleButton: Story = { | ||
render: () => { | ||
const TOGGLED = [false, true]; | ||
|
||
return html` | ||
${SCHEME.map( | ||
scheme => html` | ||
<div data-color-scheme="${scheme}" class="palette-default px-5"> | ||
${BTN.map(btn => | ||
SIZES.map(size => | ||
TOGGLED.map( | ||
isToggled => html` | ||
${meta.render({ | ||
variant: btn, | ||
size: size || 'null', | ||
toggled: isToggled, | ||
contentWhenUntoggled: 'Untoggled', | ||
contentWhenToggled: 'Toggled', | ||
})} | ||
`, | ||
), | ||
), | ||
)} | ||
</div> | ||
`, | ||
)} | ||
`; | ||
}, | ||
}; |
Oops, something went wrong.