Skip to content

Commit bd19c13

Browse files
committed
Add readonly for color swatch component
1 parent fc19b5d commit bd19c13

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

packages/uui-color-swatch/lib/uui-color-swatch.element.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,23 @@ export class UUIColorSwatchElement extends LabelMixin(
5151
private _color?: string;
5252

5353
/**
54-
* Determines if the options is disabled. If true the option can't be selected
55-
*
54+
* Sets the swatch to disabled.
55+
* @type {boolean}
5656
* @attr
57+
* @default false
5758
*/
5859
@property({ type: Boolean, reflect: true })
5960
disabled = false;
6061

62+
/**
63+
* Sets the swatch to readonly mode.
64+
* @type {boolean}
65+
* @attr
66+
* @default false
67+
*/
68+
@property({ type: Boolean, reflect: true })
69+
readonly = false;
70+
6171
/**
6272
* When true shows element label below the color checkbox
6373
*
@@ -82,10 +92,13 @@ export class UUIColorSwatchElement extends LabelMixin(
8292
}
8393

8494
willUpdate(changedProperties: Map<string, any>) {
85-
if (changedProperties.has('disabled')) {
95+
if (
96+
changedProperties.has('disabled') ||
97+
changedProperties.has('readonly')
98+
) {
8699
if (this.selectable) {
87-
this.selectable = !this.disabled;
88-
this.deselectable = !this.disabled;
100+
this.selectable = !this.disabled && !this.readonly;
101+
this.deselectable = !this.disabled && !this.readonly;
89102
}
90103
}
91104
if (
@@ -101,7 +114,8 @@ export class UUIColorSwatchElement extends LabelMixin(
101114
<button
102115
id="swatch"
103116
aria-label=${this.label}
104-
aria-disabled="${this.disabled}"
117+
aria-readonly="${this.readonly}"
118+
?disabled="${this.disabled}"
105119
title="${this.label}">
106120
<div class="color-swatch color-swatch--transparent-bg">
107121
<div
@@ -166,6 +180,10 @@ export class UUIColorSwatchElement extends LabelMixin(
166180
opacity: 0.5;
167181
}
168182
183+
:host([readonly]) {
184+
cursor: default;
185+
}
186+
169187
#swatch {
170188
cursor: inherit;
171189
outline: none;

packages/uui-color-swatch/lib/uui-color-swatch.story.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const Disabled: Story = {
5757
parameters: {
5858
docs: {
5959
source: {
60-
code: `<uui-color-swatch disabled></uui-color-swatch>`,
60+
code: `<uui-color-swatch disabled selectable></uui-color-swatch>`,
6161
},
6262
},
6363
},
@@ -78,6 +78,35 @@ export const DisabledSelected: Story = {
7878
},
7979
};
8080

81+
export const Readonly: Story = {
82+
args: {
83+
readonly: true,
84+
selectable: true,
85+
},
86+
parameters: {
87+
docs: {
88+
source: {
89+
code: `<uui-color-swatch readonly selectable></uui-color-swatch>`,
90+
},
91+
},
92+
},
93+
};
94+
95+
export const ReadonlySelected: Story = {
96+
args: {
97+
readonly: true,
98+
selectable: true,
99+
selected: true,
100+
},
101+
parameters: {
102+
docs: {
103+
source: {
104+
code: `<uui-color-swatch readonly selectable selected></uui-color-swatch>`,
105+
},
106+
},
107+
},
108+
};
109+
81110
export const WithLabel: Story = {
82111
args: {
83112
label: "This is the most beautiful color I've ever seen",

0 commit comments

Comments
 (0)