@fluid-topics/ft-switch 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,8 @@
1
+ import { PropertyValues } from "lit";
1
2
  import { ElementDefinitionsMap, FtCssVariable, FtLitElement } from "@fluid-topics/ft-wc-utils";
2
3
  import { Position } from "@fluid-topics/ft-tooltip";
3
4
  export interface FtSwitchOptionProperties {
4
- value: string;
5
+ value: any;
5
6
  icon?: string;
6
7
  selected: boolean;
7
8
  trailingIcon: boolean;
@@ -13,28 +14,22 @@ export declare const FtSwitchOptionCssVariables: {
13
14
  borderWidthRight: FtCssVariable;
14
15
  borderWidthVertical: FtCssVariable;
15
16
  };
16
- export declare class SwitchOptionSelected extends CustomEvent<void> {
17
+ export declare class SwitchOptionChange extends CustomEvent<void> {
17
18
  constructor();
18
19
  }
19
20
  export declare class FtSwitchOption extends FtLitElement implements FtSwitchOptionProperties {
20
21
  static elementDefinitions: ElementDefinitionsMap;
21
- static shadowRootOptions: {
22
- delegatesFocus: boolean;
23
- mode: ShadowRootMode;
24
- slotAssignment?: SlotAssignmentMode | undefined;
25
- customElements?: CustomElementRegistry | undefined;
26
- };
27
22
  protected getStyles(): import("lit").CSSResult;
28
- value: string;
23
+ value: any;
29
24
  icon?: string;
30
25
  label: string;
31
26
  tooltipPosition: Position;
32
27
  selected: boolean;
33
28
  trailingIcon: boolean;
34
- name: string;
35
29
  private slottedContent?;
36
30
  private input;
37
31
  focus(): void;
32
+ protected updated(props: PropertyValues<any>): void;
38
33
  protected getTemplate(): import("lit-html").TemplateResult<1>;
39
34
  private addTooltip;
40
35
  private onChange;
@@ -4,7 +4,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
- import { css, html, LitElement, nothing } from "lit";
7
+ import { css, html, nothing } from "lit";
8
8
  import { property, query } from "lit/decorators.js";
9
9
  import { customElement, designSystemVariables, FtCssVariable, FtLitElement, setVariable } from "@fluid-topics/ft-wc-utils";
10
10
  import { FtRipple, FtRippleCssVariables } from "@fluid-topics/ft-ripple";
@@ -20,9 +20,9 @@ export const FtSwitchOptionCssVariables = {
20
20
  borderWidthRight: FtCssVariable.create("--ft-switch-option-border-width-right", "SIZE", "1px"),
21
21
  borderWidthVertical: FtCssVariable.create("--ft-switch-option-border-width-vertical", "SIZE", "1px"),
22
22
  };
23
- export class SwitchOptionSelected extends CustomEvent {
23
+ export class SwitchOptionChange extends CustomEvent {
24
24
  constructor() {
25
- super("selected");
25
+ super("option-change", { bubbles: true });
26
26
  }
27
27
  }
28
28
  let FtSwitchOption = class FtSwitchOption extends FtLitElement {
@@ -33,7 +33,6 @@ let FtSwitchOption = class FtSwitchOption extends FtLitElement {
33
33
  this.tooltipPosition = "bottom";
34
34
  this.selected = false;
35
35
  this.trailingIcon = false;
36
- this.name = "";
37
36
  }
38
37
  getStyles() {
39
38
  // language=CSS
@@ -79,16 +78,6 @@ let FtSwitchOption = class FtSwitchOption extends FtLitElement {
79
78
  opacity: ${designSystemVariables.opacityPrimaryOnSurfaceSelected};
80
79
  }
81
80
 
82
- .ft-switch-option--input:focus + .ft-switch-option--content:before {
83
- background-color: ${FtSwitchCssVariables.selectedBackgroundColor};
84
- border-radius: ${FtSwitchOptionCssVariables.borderRadiusLeft} ${FtSwitchOptionCssVariables.borderRadiusRight} ${FtSwitchOptionCssVariables.borderRadiusRight} ${FtSwitchOptionCssVariables.borderRadiusLeft};
85
- position: absolute;
86
- inset: 0;
87
- content: "";
88
- z-index: 1;
89
- opacity: ${designSystemVariables.opacityPrimaryOnSurfaceFocused};
90
- }
91
-
92
81
  .ft-switch-option--label {
93
82
  color: ${FtSwitchCssVariables.textColor};
94
83
  overflow: hidden;
@@ -134,6 +123,12 @@ let FtSwitchOption = class FtSwitchOption extends FtLitElement {
134
123
  focus() {
135
124
  this.input.focus();
136
125
  }
126
+ updated(props) {
127
+ super.updated(props);
128
+ if (props.has("selected")) {
129
+ this.dispatchEvent(new SwitchOptionChange());
130
+ }
131
+ }
137
132
  getTemplate() {
138
133
  const classes = {
139
134
  "ft-switch-option": true,
@@ -143,8 +138,6 @@ let FtSwitchOption = class FtSwitchOption extends FtLitElement {
143
138
  <label for="input" class=${classMap(classes)}>
144
139
  <input id="input" type="radio" class="ft-switch-option--input"
145
140
  .checked=${this.selected}
146
- .value=${this.value}
147
- .name=${this.name}
148
141
  @change=${this.onChange}>
149
142
  <div class="ft-switch-option--content">
150
143
  <ft-ripple part="ripple" class="ft-switch-option--ripple"></ft-ripple>
@@ -168,7 +161,6 @@ let FtSwitchOption = class FtSwitchOption extends FtLitElement {
168
161
  }
169
162
  onChange() {
170
163
  this.selected = true;
171
- this.dispatchEvent(new SwitchOptionSelected());
172
164
  }
173
165
  getLabel() {
174
166
  return this.label || this.textContent;
@@ -196,7 +188,6 @@ FtSwitchOption.elementDefinitions = {
196
188
  "ft-tooltip": FtTooltip,
197
189
  "mwc-icon": Icon
198
190
  };
199
- FtSwitchOption.shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
200
191
  __decorate([
201
192
  property({ type: String })
202
193
  ], FtSwitchOption.prototype, "value", void 0);
@@ -215,9 +206,6 @@ __decorate([
215
206
  __decorate([
216
207
  property({ type: Boolean })
217
208
  ], FtSwitchOption.prototype, "trailingIcon", void 0);
218
- __decorate([
219
- property({ attribute: false })
220
- ], FtSwitchOption.prototype, "name", void 0);
221
209
  __decorate([
222
210
  query(".ft-switch-option--content slot")
223
211
  ], FtSwitchOption.prototype, "slottedContent", void 0);
@@ -1,3 +1,4 @@
1
+ import { PropertyValues } from "lit";
1
2
  import { ElementDefinitionsMap, FtCssVariable, FtLitElement } from "@fluid-topics/ft-wc-utils";
2
3
  export * from "./ft-switch-option";
3
4
  export interface FtSwitchProperties {
@@ -14,25 +15,25 @@ export declare const FtSwitchCssVariables: {
14
15
  iconSize: FtCssVariable;
15
16
  iconColor: FtCssVariable;
16
17
  rippleColor: FtCssVariable;
18
+ outlineColor: FtCssVariable;
17
19
  };
18
- export declare class FtSwitchChange extends CustomEvent<string> {
19
- constructor(value: string);
20
+ export declare class FtSwitchChange extends CustomEvent<any> {
21
+ constructor(value: any);
20
22
  }
21
23
  export declare class FtSwitch extends FtLitElement implements FtSwitchProperties {
22
24
  static elementDefinitions: ElementDefinitionsMap;
23
- constructor();
24
- get value(): string | undefined;
25
+ get value(): any;
25
26
  private slottedContent?;
26
27
  private selectedOption?;
27
28
  private options;
28
- private groupName;
29
+ protected updated(props: PropertyValues<any>): void;
29
30
  protected getStyles(): import("lit").CSSResult;
30
31
  protected getTemplate(): import("lit-html").TemplateResult<1>;
31
32
  private onKeyDown;
32
- private onFocus;
33
+ private fixIndex;
33
34
  private onSlotchange;
34
35
  private registerOption;
35
- private selectOption;
36
+ private onOptionChange;
36
37
  private updateOptions;
37
38
  }
38
39
  //# sourceMappingURL=ft-switch.d.ts.map
@@ -5,8 +5,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
7
  import { css, html } from "lit";
8
- import { query } from "lit/decorators.js";
9
- import { v4 as uuidv4 } from "uuid";
8
+ import { query, state } from "lit/decorators.js";
10
9
  import { customElement, designSystemVariables, FtCssVariable, FtLitElement, setVariable } from "@fluid-topics/ft-wc-utils";
11
10
  import { FtSwitchOption, FtSwitchOptionCssVariables } from "./ft-switch-option";
12
11
  import { FtTypographyButtonCssVariables } from "@fluid-topics/ft-typography";
@@ -22,6 +21,7 @@ export const FtSwitchCssVariables = {
22
21
  iconSize: FtCssVariable.create("--ft-switch-icon-size", "SIZE", "24px"),
23
22
  iconColor: FtCssVariable.extend("--ft-switch-text-color", designSystemVariables.colorOnSurfaceMedium),
24
23
  rippleColor: FtCssVariable.extend("--ft-switch-ripple-color", designSystemVariables.colorPrimary),
24
+ outlineColor: FtCssVariable.extend("--ft-switch-outline-color", designSystemVariables.colorPrimary),
25
25
  };
26
26
  export class FtSwitchChange extends CustomEvent {
27
27
  constructor(value) {
@@ -30,15 +30,21 @@ export class FtSwitchChange extends CustomEvent {
30
30
  }
31
31
  let FtSwitch = class FtSwitch extends FtLitElement {
32
32
  constructor() {
33
- super();
33
+ super(...arguments);
34
34
  this.options = [];
35
- this.groupName = uuidv4();
36
- this.addEventListener("keydown", (e) => this.onKeyDown(e));
37
35
  }
38
36
  get value() {
39
37
  var _a;
40
38
  return (_a = this.selectedOption) === null || _a === void 0 ? void 0 : _a.value;
41
39
  }
40
+ updated(props) {
41
+ var _a;
42
+ super.updated(props);
43
+ if (props.has("selectedOption")) {
44
+ this.updateOptions();
45
+ this.dispatchEvent(new FtSwitchChange((_a = this.selectedOption) === null || _a === void 0 ? void 0 : _a.value));
46
+ }
47
+ }
42
48
  getStyles() {
43
49
  // language=CSS
44
50
  return css `
@@ -46,6 +52,11 @@ let FtSwitch = class FtSwitch extends FtLitElement {
46
52
  display: inline-block;
47
53
  }
48
54
 
55
+ .ft-switch {
56
+ border-radius: ${FtSwitchCssVariables.borderRadius};
57
+ outline-color: ${FtSwitchCssVariables.outlineColor};
58
+ }
59
+
49
60
  .ft-switch slot {
50
61
  display: flex;
51
62
  align-items: center;
@@ -69,61 +80,56 @@ let FtSwitch = class FtSwitch extends FtLitElement {
69
80
  }
70
81
  getTemplate() {
71
82
  return html `
72
- <div class="ft-switch">
73
- <slot @slotchange=${this.onSlotchange}></slot>
83
+ <div class="ft-switch" @keydown=${this.onKeyDown} tabindex="0">
84
+ <slot @slotchange=${this.onSlotchange}
85
+ @option-change=${this.onOptionChange}></slot>
74
86
  </div>
75
87
  `;
76
88
  }
77
89
  onKeyDown(e) {
78
- let focusedOption = this.options.find(option => option.ownerDocument.activeElement == option);
79
- if (focusedOption) {
80
- if (e.key == "ArrowDown" || e.key == "ArrowRight") {
81
- let optionIndex = this.options.indexOf(focusedOption);
82
- if (optionIndex < this.options.length - 1) {
83
- optionIndex++;
84
- }
85
- e.preventDefault();
86
- this.selectOption(this.options[optionIndex]);
87
- }
88
- if (e.key == "ArrowUp" || e.key == "ArrowLeft") {
89
- let optionIndex = this.options.indexOf(focusedOption);
90
- if (optionIndex > 0) {
91
- optionIndex--;
92
- }
93
- e.preventDefault();
94
- this.selectOption(this.options[optionIndex]);
95
- }
90
+ let newIndex = undefined;
91
+ if (e.key == "ArrowDown" || e.key == "ArrowRight") {
92
+ newIndex = this.selectedOption ? this.options.indexOf(this.selectedOption) + 1 : 0;
93
+ }
94
+ if (e.key == "ArrowUp" || e.key == "ArrowLeft") {
95
+ newIndex = this.selectedOption ? this.options.indexOf(this.selectedOption) - 1 : this.options.length - 1;
96
+ }
97
+ if (newIndex != undefined) {
98
+ e.preventDefault();
99
+ this.options[this.fixIndex(newIndex)].selected = true;
96
100
  }
97
101
  }
98
- onFocus(option) {
99
- if (this.selectedOption && this.selectedOption != option) {
100
- this.selectedOption.focus();
102
+ fixIndex(index) {
103
+ if (index < 0) {
104
+ return this.options.length - 1;
105
+ }
106
+ if (index > this.options.length - 1) {
107
+ return 0;
101
108
  }
109
+ return index;
102
110
  }
103
111
  onSlotchange() {
104
112
  var _a;
105
113
  this.options = [];
106
- return (_a = this.slottedContent) === null || _a === void 0 ? void 0 : _a.assignedElements().forEach(element => {
114
+ (_a = this.slottedContent) === null || _a === void 0 ? void 0 : _a.assignedElements().forEach(element => {
107
115
  if (element instanceof FtSwitchOption) {
108
116
  this.registerOption(element);
109
117
  }
110
- this.updateOptions();
111
118
  });
119
+ this.selectedOption = this.options.find(option => option.selected);
112
120
  }
113
121
  registerOption(option) {
114
- option.name = this.groupName;
115
- option.addEventListener("selected", () => this.selectOption(option));
116
- option.addEventListener("focus", () => this.onFocus(option));
122
+ option.setAttribute("tabindex", "-1");
123
+ this.options.push(option);
124
+ }
125
+ onOptionChange(e) {
126
+ let option = e.target;
117
127
  if (option.selected) {
118
128
  this.selectedOption = option;
119
129
  }
120
- this.options.push(option);
121
- }
122
- selectOption(option) {
123
- this.selectedOption = option;
124
- option.focus();
125
- this.dispatchEvent(new FtSwitchChange(this.selectedOption.value));
126
- this.updateOptions();
130
+ else {
131
+ this.selectedOption = this.options.find(option => option.selected);
132
+ }
127
133
  }
128
134
  updateOptions() {
129
135
  this.options.forEach(option => {
@@ -135,6 +141,9 @@ FtSwitch.elementDefinitions = {};
135
141
  __decorate([
136
142
  query(".ft-switch slot")
137
143
  ], FtSwitch.prototype, "slottedContent", void 0);
144
+ __decorate([
145
+ state()
146
+ ], FtSwitch.prototype, "selectedOption", void 0);
138
147
  FtSwitch = __decorate([
139
148
  customElement("ft-switch")
140
149
  ], FtSwitch);