Skip to content

Commit a40b6b3

Browse files
committed
fix(list): fixed ListItem disabled colors to optionally include addons
The new `disabledOpacity` prop will allow for the full ListItem to gain an opacity value while disabled instead of only setting the color values. This allows the addons to also be dimmed. fix #997
1 parent 06e91ca commit a40b6b3

6 files changed

Lines changed: 58 additions & 1 deletion

File tree

packages/list/src/SimpleListItem.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const SimpleListItem = forwardRef<HTMLLIElement, SimpleListItemProps>(
3939
clickable = false,
4040
onClick,
4141
disabled = false,
42+
disabledOpacity = false,
4243
...props
4344
},
4445
ref
@@ -66,6 +67,8 @@ export const SimpleListItem = forwardRef<HTMLLIElement, SimpleListItemProps>(
6667
"three-lines": threeLines,
6768
clickable,
6869
disabled: isDisabled,
70+
"disabled-color": isDisabled && !disabledOpacity,
71+
"disabled-opacity": isDisabled && disabledOpacity,
6972
}),
7073
className
7174
)}

packages/list/src/__tests__/ListItem.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,17 @@ describe("ListItem", () => {
4949
rerender(<ListItem {...props} tabIndex={0} disabled />);
5050
expect(item.tabIndex).toBe(0);
5151
});
52+
53+
it("should apply the correct disabled classes based on the disabledOpacity prop", () => {
54+
const props = { disabled: true, children: "Content" };
55+
const { rerender, getByRole } = render(<ListItem {...props} />);
56+
57+
const item = getByRole("button");
58+
expect(item.className).toContain("rmd-list-item--disabled-color");
59+
expect(item.className).not.toContain("rmd-list-item--disabled-opacity");
60+
61+
rerender(<ListItem {...props} disabledOpacity />);
62+
expect(item.className).not.toContain("rmd-list-item--disabled-color");
63+
expect(item.className).toContain("rmd-list-item--disabled-opacity");
64+
});
5265
});

packages/list/src/__tests__/SimpleListItem.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ describe("SimpleListItem", () => {
3939
fireEvent.click(item);
4040
expect(onClick).toBeCalled();
4141
});
42+
43+
it("should apply the correct disabled classes based on the disabledOpacity prop", () => {
44+
const props = { role: "button", disabled: true, children: "Content" };
45+
const { rerender, getByRole } = render(<SimpleListItem {...props} />);
46+
47+
const item = getByRole("button");
48+
expect(item.className).toContain("rmd-list-item--disabled-color");
49+
expect(item.className).not.toContain("rmd-list-item--disabled-opacity");
50+
51+
rerender(<SimpleListItem {...props} disabledOpacity />);
52+
expect(item.className).not.toContain("rmd-list-item--disabled-color");
53+
expect(item.className).toContain("rmd-list-item--disabled-opacity");
54+
});
4255
});

packages/list/src/_mixins.scss

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,19 @@
184184
}
185185

186186
&--disabled {
187+
pointer-events: none;
188+
}
189+
190+
&--disabled-color {
187191
@include rmd-theme(color, text-disabled-on-background);
192+
@include rmd-theme-update-var(
193+
text-secondary-on-background,
194+
rmd-theme-var(text-disabled-on-background)
195+
);
196+
}
188197

189-
pointer-events: none;
198+
&--disabled-opacity {
199+
opacity: $rmd-list-item-disabled-opacity;
190200
}
191201

192202
&--link {

packages/list/src/_variables.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ $rmd-list-item-media-large-size: 6.25rem !default;
158158
/// @type Number
159159
$rmd-list-item-media-spacing: 1rem !default;
160160

161+
/// The opacity to apply to a list item when it is `disabled` and the
162+
/// `disabledOpacity` boolean is enabled that will also darken any addons
163+
/// rendered in the list item.
164+
///
165+
/// @type Number
166+
$rmd-list-item-disabled-opacity: 0.5 !default;
167+
161168
/// A Map of all the "themeable" parts of the list package. Every key in this
162169
/// map will be used to create a css variable to dynamically update the values
163170
/// of the icon as needed.

packages/list/src/getListItemHeight.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ export interface SimpleListItemProps
1818
*/
1919
disabled?: boolean;
2020

21+
/**
22+
* Boolean if the list item should apply an opacity value while disabled
23+
* instead of overriding the primary and secondary text colors. Enabling this
24+
* will allow for the list item addons to also be dimmed.
25+
*
26+
* This is configured by the `$rmd-list-item-disabled-opacity` variable.
27+
*
28+
* Note: This does nothing if the `disabled` prop is not enabled.
29+
*/
30+
disabledOpacity?: boolean;
31+
2132
/**
2233
* Boolean if the list item should be updated to use the clickable styles to
2334
* the item. This is really just a pass-down value for the main `ListItem`

0 commit comments

Comments
 (0)