Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add switch component

Benjamin Bädorfa86818119411
diff --git a/frontend/src/app/spot/components/checkbox/stories/Checkbox.stories.mdx b/frontend/src/app/spot/components/checkbox/stories/Checkbox.stories.mdx
index 50327023f3d..674c6c93a83 100644
--- a/frontend/src/app/spot/components/checkbox/stories/Checkbox.stories.mdx
+++ b/frontend/src/app/spot/components/checkbox/stories/Checkbox.stories.mdx
@@ -30,16 +30,6 @@ This component describes only the actual checkbox, without the label. For the fu
The selector field itself only has two states, *enabled* and *disabled*.
-export const HTML = require('!!raw-loader!./CheckboxHTML.html').default;
-
-<Canvas>
- <Story name="HTML Template">
- {{ template: HTML }}
- </Story>
-</Canvas>
-
-<ArgsTable of={SpotCheckboxComponent} />
-
export const Angular = ({ checked, disabled }) => ({
props: { checked, disabled },
});
@@ -49,3 +39,5 @@ export const Angular = ({ checked, disabled }) => ({
{Angular.bind({ checked: true, disabled: false })}
</Story>
</Canvas>
+
+<ArgsTable of={SpotCheckboxComponent} />
diff --git a/frontend/src/app/spot/components/checkbox/stories/CheckboxHTML.html b/frontend/src/app/spot/components/checkbox/stories/CheckboxHTML.html
deleted file mode 100644
index f4b9c262cf6..00000000000
--- a/frontend/src/app/spot/components/checkbox/stories/CheckboxHTML.html
+++ /dev/null
@@ -1,7 +0,0 @@
-<label class="spot-checkbox">
- <input
- type="checkbox"
- class="spot-checkbox--input"
- />
- <span class="spot-checkbox--fake"></span>
-</label>
diff --git a/frontend/src/app/spot/components/switch/stories/Switch.stories.mdx b/frontend/src/app/spot/components/switch/stories/Switch.stories.mdx
new file mode 100644
index 00000000000..f64d706ab3c
--- /dev/null
+++ b/frontend/src/app/spot/components/switch/stories/Switch.stories.mdx
@@ -0,0 +1,43 @@
+import { moduleMetadata } from '@storybook/angular';
+import { Canvas, Meta, Story, ArgsTable } from '@storybook/addon-docs';
+
+import { OpSpotModule } from '../../../spot.module';
+import { SpotSwitchComponent } from '../switch.component';
+
+<Meta
+ title="Components/Switch"
+ component={SpotSwitchComponent}
+ decorators={[
+ moduleMetadata({
+ imports: [
+ OpSpotModule,
+ ],
+ }),
+ ]}
+ parameters={{
+ design: {
+ type: 'figma',
+ url: 'https://www.figma.com/file/XhCsrvs6rePifqbBpKYRWD/Components-Library?node-id=1785%3A6910',
+ },
+ }}
+/>
+
+# Switches
+
+This component describes only the actual switch, without the label. For the full component, please refer to Selector field component, which provides a label.
+
+## States
+
+The selector field itself only has two states, *enabled* and *disabled*.
+
+export const Angular = ({ checked, disabled }) => ({
+ props: { checked, disabled },
+});
+
+<Canvas>
+ <Story name="Angular component">
+ {Angular.bind({ checked: true, disabled: false })}
+ </Story>
+</Canvas>
+
+<ArgsTable of={SpotSwitchComponent} />
diff --git a/frontend/src/app/spot/components/switch/stories/SwitchAngular.html b/frontend/src/app/spot/components/switch/stories/SwitchAngular.html
new file mode 100644
index 00000000000..4194a7a077f
--- /dev/null
+++ b/frontend/src/app/spot/components/switch/stories/SwitchAngular.html
@@ -0,0 +1,8 @@
+<label>
+ <spot-switch
+ [name]="name"
+ [disabled]="disabled"
+ [checked]="checked"
+ (change)="checkedChange"
+ ></spot-switch>
+</label>
diff --git a/frontend/src/app/spot/components/switch/stories/SwitchAngular.ts b/frontend/src/app/spot/components/switch/stories/SwitchAngular.ts
new file mode 100644
index 00000000000..390fc299320
--- /dev/null
+++ b/frontend/src/app/spot/components/switch/stories/SwitchAngular.ts
@@ -0,0 +1,19 @@
+import {
+ Component,
+ EventEmitter,
+ Input,
+ Output,
+} from '@angular/core';
+
+@Component({
+ templateUrl: './SwitchAngular.html',
+})
+export class SwitchAngularStoryComponent {
+ @Input() disabled = false;
+
+ @Input() name = `spot-switch-${+(new Date())}`;
+
+ @Input() public checked = false;
+
+ @Output() checkedChange = new EventEmitter<boolean>();
+}
diff --git a/frontend/src/app/spot/components/switch/switch.component.html b/frontend/src/app/spot/components/switch/switch.component.html
new file mode 100644
index 00000000000..75662dbf36a
--- /dev/null
+++ b/frontend/src/app/spot/components/switch/switch.component.html
@@ -0,0 +1,11 @@
+<input
+ type="checkbox"
+ class="spot-switch--input"
+ [attr.name]="name"
+ [disabled]="disabled"
+ [checked]="checked"
+ (change)="onStateChange()"
+ [tabindex]="tabindex"
+ #input
+/>
+<span class="spot-switch--fake"></span>
diff --git a/frontend/src/app/spot/components/switch/switch.component.ts b/frontend/src/app/spot/components/switch/switch.component.ts
new file mode 100644
index 00000000000..e0007d7f09d
--- /dev/null
+++ b/frontend/src/app/spot/components/switch/switch.component.ts
@@ -0,0 +1,75 @@
+import {
+ Component,
+ ElementRef,
+ EventEmitter,
+ forwardRef,
+ HostBinding,
+ Input,
+ Output,
+ ViewChild,
+} from '@angular/core';
+import {
+ ControlValueAccessor,
+ NG_VALUE_ACCESSOR,
+} from '@angular/forms';
+
+export type SpotSwitchState = boolean;
+
+@Component({
+ selector: 'spot-switch',
+ templateUrl: './switch.component.html',
+ providers: [{
+ provide: NG_VALUE_ACCESSOR,
+ useExisting: forwardRef(() => SpotSwitchComponent),
+ multi: true,
+ }],
+})
+export class SpotSwitchComponent implements ControlValueAccessor {
+ @HostBinding('class.spot-switch') public className = true;
+
+ @ViewChild('input') public input:ElementRef;
+
+ @Input() tabindex = 0;
+
+ @Input() disabled = false;
+
+ @Input() name = `spot-switch-${+(new Date())}`;
+
+ @Output() checkedChange = new EventEmitter<boolean>();
+
+ @Input() public checked = false;
+
+ onStateChange():void {
+ const value = (this.input.nativeElement as HTMLInputElement).checked;
+ this.checkedChange.emit(value);
+ this.onChange(value);
+ this.onTouched(value);
+ }
+
+ writeValue(value:SpotSwitchState):void {
+ // This is set in a timeout because the initial value is set before the template is ready,
+ // which causes the input nativeElement to not be available yet.
+ setTimeout(() => {
+ const input = this.input.nativeElement as HTMLInputElement;
+ if (value === null) {
+ input.indeterminate = true;
+ } else {
+ input.indeterminate = false;
+ }
+
+ this.checked = !!value;
+ });
+ }
+
+ onChange = (_:SpotSwitchState):void => {};
+
+ onTouched = (_:SpotSwitchState):void => {};
+
+ registerOnChange(fn:(_:SpotSwitchState) => void):void {
+ this.onChange = fn;
+ }
+
+ registerOnTouched(fn:(_:SpotSwitchState) => void):void {
+ this.onTouched = fn;
+ }
+}
diff --git a/frontend/src/app/spot/styles/sass/components/index.sass b/frontend/src/app/spot/styles/sass/components/index.sass
index 778eada86c6..c63ce1978e0 100644
--- a/frontend/src/app/spot/styles/sass/components/index.sass
+++ b/frontend/src/app/spot/styles/sass/components/index.sass
@@ -5,6 +5,7 @@
@import './link'
@import './toggle'
@import './checkbox'
+@import './switch'
@import './text-field'
@import './filter-chip'
@import './drop-modal'
diff --git a/frontend/src/app/spot/styles/sass/components/switch.sass b/frontend/src/app/spot/styles/sass/components/switch.sass
new file mode 100644
index 00000000000..7084b02b472
--- /dev/null
+++ b/frontend/src/app/spot/styles/sass/components/switch.sass
@@ -0,0 +1,59 @@
+.spot-switch
+ display: inline-flex
+ justify-content: center
+ align-items: center
+ height: $spot-spacing-1 + $spot-spacing-0_25
+ width: $spot-spacing-2 + $spot-spacing-0_25
+ flex-shrink: 0
+
+ &:focus-within &--fake
+ @include spot-focus
+
+ &, *
+ box-sizing: border-box
+
+ &--input
+ position: fixed
+ height: 1px
+ width: 1px
+ overflow: hidden
+ clip: rect(1px,1px,1px,1px)
+
+ &--fake
+ position: relative
+ box-sizing: border-box
+ display: flex
+ justify-content: center
+ align-items: center
+ height: $spot-spacing-1 + $spot-spacing-0_25
+ width: $spot-spacing-2 + $spot-spacing-0_25
+ border-radius: ($spot-spacing-1 + $spot-spacing-0_25) / 2
+ border: 1px solid $spot-color-basic-gray-3
+ background-color: $spot-color-basic-gray-5
+ cursor: pointer
+
+ &::before
+ display: block
+ content: ''
+ position: absolute
+ top: 1px
+ left: 1px
+ height: $spot-spacing-1
+ width: $spot-spacing-1
+ background-color: $spot-color-basic-gray-3
+ border-radius: 50%
+ transition-property: left, background-color
+ transition-duration: 0.2s
+ transition-timing-function: ease
+
+ &--input:checked + &--fake
+ background-color: $spot-color-main-light
+
+ &--input:checked + &--fake::before
+ left: calc(100% - #{$spot-spacing-1} - 1px)
+ background-color: $spot-color-main
+
+ &--input:disabled + &--fake,
+ &--input:disabled + &--fake::before
+ opacity: 0.5
+ cursor: default