-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(design): add switch component (#3143)
Co-authored-by: xelaint <xelaint@gmail.com> Co-authored-by: Damien Retzinger <damienwebdev@gmail.com>
- Loading branch information
1 parent
eb5e20b
commit 8879733
Showing
37 changed files
with
659 additions
and
2 deletions.
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
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
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,17 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { | ||
Routes, | ||
RouterModule, | ||
} from '@angular/router'; | ||
|
||
import { DesignLandSwitchComponent } from './switch.component'; | ||
|
||
const routes: Routes = [ | ||
{ path: '', component: DesignLandSwitchComponent }, | ||
]; | ||
|
||
@NgModule({ | ||
imports: [RouterModule.forChild(routes)], | ||
exports: [RouterModule], | ||
}) | ||
export class DesignLandSwitchRoutingModule { } |
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 @@ | ||
<h1>Switch</h1> | ||
|
||
<design-land-example-viewer-container example="basic-switch"></design-land-example-viewer-container> | ||
<design-land-example-viewer-container example="disabled-switch"></design-land-example-viewer-container> | ||
<design-land-example-viewer-container example="loading-switch"></design-land-example-viewer-container> | ||
<design-land-example-viewer-container example="switch-error"></design-land-example-viewer-container> | ||
<design-land-example-viewer-container example="switch-label-positions"></design-land-example-viewer-container> |
Empty file.
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 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'design-land-switch', | ||
templateUrl: './switch.component.html', | ||
}) | ||
export class DesignLandSwitchComponent {} |
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,23 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
|
||
import { DaffArticleModule } from '@daffodil/design/article'; | ||
|
||
import { DesignLandSwitchRoutingModule } from './switch-routing.module'; | ||
import { DesignLandSwitchComponent } from './switch.component'; | ||
import { DesignLandArticleEncapsulatedModule } from '../core/article-encapsulated/article-encapsulated.module'; | ||
import { DesignLandExampleViewerModule } from '../core/code-preview/container/example-viewer.module'; | ||
|
||
@NgModule({ | ||
declarations: [ | ||
DesignLandSwitchComponent, | ||
], | ||
imports: [ | ||
CommonModule, | ||
DaffArticleModule, | ||
DesignLandExampleViewerModule, | ||
DesignLandSwitchRoutingModule, | ||
DesignLandArticleEncapsulatedModule, | ||
], | ||
}) | ||
export class DesignLandSwitchModule { } |
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
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,61 @@ | ||
# Switch | ||
A switch allows users to toggle the state of a single setting. | ||
|
||
## Overview | ||
Switches provide a way to toggle between two states with a visual indicator of the current state. A label can be positioned on the left, right, top, or bottom of the switch. The switch can be set to a disabled state, display a loading indicator, or show a custom error message. | ||
|
||
## Usage | ||
|
||
### Within a standalone component | ||
To use a switch in a standalone component, import it directly into your custom component: | ||
|
||
```ts | ||
@Component({ | ||
selector: 'custom-component', | ||
templateUrl: './custom-component.component.html', | ||
standalone: true, | ||
imports: [ | ||
DAFF_SWITCH_COMPONENTS, | ||
], | ||
}) | ||
export class CustomComponent {} | ||
``` | ||
|
||
## Examples | ||
|
||
### Basic Switch | ||
A basic switch can be toggled by setting the `checked` property to `true` or `false`. By default, this is set to `false`. | ||
|
||
<design-land-example-viewer-container example="basic-switch"></design-land-example-viewer-container> | ||
|
||
### Disabled Switch | ||
A switch with the `disabled` property will be non-interactive. | ||
|
||
<design-land-example-viewer-container example="disabled-switch"></design-land-example-viewer-container> | ||
|
||
### Loading Switch | ||
A switch can display a loading state by setting `loading` to `true`. This will also disable the switch. | ||
|
||
<design-land-example-viewer-container example="loading-switch"></design-land-example-viewer-container> | ||
|
||
### Switch with Error | ||
An error message can be displayed by setting `error` to `true` and including a `daff-error-message` to show the message text. | ||
|
||
<design-land-example-viewer-container example="switch-error"></design-land-example-viewer-container> | ||
|
||
### Changing Label Position | ||
The label position can be changed by setting the `labelPosition` property. The default position is `left`. | ||
|
||
Supported positions: `left | right | top | bottom` | ||
|
||
<design-land-example-viewer-container example="switch-label-positions"></design-land-example-viewer-container> | ||
|
||
|
||
## Accessibility | ||
Switches follow the [ARIA Switch design pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/). | ||
|
||
### Keyboard Interactions | ||
| Key | Action | | ||
| --- | ------ | | ||
| Space | Toggles the switch to the opposite state. | | ||
| Tab | Moves to next component on the page. | |
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 @@ | ||
{ | ||
"$schema": "../../../../node_modules/ng-packagr/ng-entrypoint.schema.json", | ||
"lib": { | ||
"entryFile": "src/index.ts", | ||
"styleIncludePaths": ["../../scss"] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
libs/design/switch/examples/src/basic-switch/basic-switch.component.html
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 @@ | ||
<daff-switch [checked]="checked">Default</daff-switch> |
34 changes: 34 additions & 0 deletions
34
libs/design/switch/examples/src/basic-switch/basic-switch.component.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,34 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
} from '@angular/core'; | ||
import { | ||
UntypedFormControl, | ||
ReactiveFormsModule, | ||
} from '@angular/forms'; | ||
|
||
import { DAFF_SWITCH_COMPONENTS } from '@daffodil/design/switch'; | ||
|
||
@Component({ | ||
// eslint-disable-next-line @angular-eslint/component-selector | ||
selector: 'basic-switch', | ||
templateUrl: './basic-switch.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
standalone: true, | ||
imports: [ | ||
DAFF_SWITCH_COMPONENTS, | ||
ReactiveFormsModule, | ||
], | ||
styles: [` | ||
:host { | ||
display: flex; | ||
flex-wrap: wrap; | ||
} | ||
`], | ||
}) | ||
export class BasicSwitchComponent { | ||
checked = false; | ||
disabled = false; | ||
loading = true; | ||
} |
1 change: 1 addition & 0 deletions
1
libs/design/switch/examples/src/disabled-switch/disabled-switch.component.html
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 @@ | ||
<daff-switch [checked]="checked" disabled>Disabled</daff-switch> |
27 changes: 27 additions & 0 deletions
27
libs/design/switch/examples/src/disabled-switch/disabled-switch.component.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,27 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
} from '@angular/core'; | ||
|
||
import { DAFF_SWITCH_COMPONENTS } from '@daffodil/design/switch'; | ||
|
||
@Component({ | ||
// eslint-disable-next-line @angular-eslint/component-selector | ||
selector: 'disabled-switch', | ||
templateUrl: './disabled-switch.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
standalone: true, | ||
imports: [ | ||
DAFF_SWITCH_COMPONENTS, | ||
], | ||
styles: [` | ||
:host { | ||
display: flex; | ||
flex-wrap: wrap; | ||
} | ||
`], | ||
}) | ||
export class DisabledSwitchComponent { | ||
checked = false; | ||
disabled = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { BasicSwitchComponent } from './basic-switch/basic-switch.component'; | ||
import { DisabledSwitchComponent } from './disabled-switch/disabled-switch.component'; | ||
import { LoadingSwitchComponent } from './loading-switch/loading-switch.component'; | ||
import { SwitchErrorComponent } from './switch-error/switch-error.component'; | ||
import { SwitchLabelPositionsComponent } from './switch-label-positions/switch-label-positions.component'; | ||
|
||
export const SWITCH_EXAMPLES = [ | ||
BasicSwitchComponent, | ||
DisabledSwitchComponent, | ||
LoadingSwitchComponent, | ||
SwitchErrorComponent, | ||
SwitchLabelPositionsComponent, | ||
]; |
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 @@ | ||
export * from './public_api'; |
1 change: 1 addition & 0 deletions
1
libs/design/switch/examples/src/loading-switch/loading-switch.component.html
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 @@ | ||
<daff-switch [checked]="checked" loading="true">Loading</daff-switch> |
27 changes: 27 additions & 0 deletions
27
libs/design/switch/examples/src/loading-switch/loading-switch.component.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,27 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
} from '@angular/core'; | ||
|
||
import { DAFF_SWITCH_COMPONENTS } from '@daffodil/design/switch'; | ||
|
||
@Component({ | ||
// eslint-disable-next-line @angular-eslint/component-selector | ||
selector: 'loading-switch', | ||
templateUrl: './loading-switch.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
standalone: true, | ||
imports: [ | ||
DAFF_SWITCH_COMPONENTS, | ||
], | ||
styles: [` | ||
:host { | ||
display: flex; | ||
flex-wrap: wrap; | ||
} | ||
`], | ||
}) | ||
export class LoadingSwitchComponent { | ||
checked = false; | ||
loading = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { SWITCH_EXAMPLES } from './examples'; | ||
|
||
export { BasicSwitchComponent } from './basic-switch/basic-switch.component'; | ||
export { DisabledSwitchComponent } from './disabled-switch/disabled-switch.component'; | ||
export { LoadingSwitchComponent } from './loading-switch/loading-switch.component'; | ||
export { SwitchErrorComponent } from './switch-error/switch-error.component'; | ||
export { SwitchLabelPositionsComponent } from './switch-label-positions/switch-label-positions.component'; |
4 changes: 4 additions & 0 deletions
4
libs/design/switch/examples/src/switch-error/switch-error.component.html
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,4 @@ | ||
<daff-switch [checked]="checked" [error]="true"> | ||
Notifications | ||
<daff-error-message>Error message</daff-error-message> | ||
</daff-switch> |
32 changes: 32 additions & 0 deletions
32
libs/design/switch/examples/src/switch-error/switch-error.component.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,32 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
} from '@angular/core'; | ||
import { | ||
UntypedFormControl, | ||
ReactiveFormsModule, | ||
} from '@angular/forms'; | ||
|
||
|
||
import { DAFF_SWITCH_COMPONENTS } from '@daffodil/design/switch'; | ||
@Component({ | ||
// eslint-disable-next-line @angular-eslint/component-selector | ||
selector: 'switch-error', | ||
templateUrl: './switch-error.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
standalone: true, | ||
imports: [ | ||
DAFF_SWITCH_COMPONENTS, | ||
ReactiveFormsModule, | ||
], | ||
styles: [` | ||
:host { | ||
display: flex; | ||
flex-wrap: wrap; | ||
} | ||
`], | ||
}) | ||
export class SwitchErrorComponent { | ||
checked = false; | ||
error = false; | ||
} |
5 changes: 5 additions & 0 deletions
5
libs/design/switch/examples/src/switch-label-positions/switch-label-positions.component.html
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,5 @@ | ||
<daff-switch [labelPosition]="labelPositionControl.value" [formControl]="labelSwitchExample" ngDefaultControl>Label</daff-switch> | ||
|
||
<select [formControl]="labelPositionControl"> | ||
<option *ngFor="let option of options" [value]="option.value">{{ option.label }}</option> | ||
</select> |
Oops, something went wrong.