Skip to content

Commit 86cd9cd

Browse files
authored
feat(design): deprecate DaffColorable and clean up documentation (#4422)
1 parent 4fc4df3 commit 86cd9cd

5 files changed

Lines changed: 108 additions & 97 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Colorable
2+
3+
Colorable enforces consistent use of color across components.
4+
5+
## Overview
6+
7+
`DaffColorableDirective` applies color-specific CSS classes and validates the color in dev mode. When a color is set, the corresponding `daff-[color]` class is added.
8+
9+
## Supported colors
10+
11+
| Color | CSS Class |
12+
|---|---|
13+
| `primary` | `.daff-primary` |
14+
| `secondary` | `.daff-secondary` |
15+
| `tertiary` | `.daff-tertiary` |
16+
| `light` | `.daff-light` |
17+
| `dark` | `.daff-dark` |
18+
| `theme` | `.daff-theme` |
19+
| `theme-contrast` | `.daff-theme-contrast` |
20+
21+
## Usage
22+
23+
Use `daffColorable` as an attribute selector:
24+
25+
```html
26+
<div daffColorable [color]="'primary'">Colored content</div>
27+
```
28+
29+
Or as an Angular host directive:
30+
31+
```ts
32+
import { DaffColorableDirective } from '@daffodil/design';
33+
34+
@Component({
35+
selector: 'custom-component',
36+
template: 'custom-component.html',
37+
hostDirectives: [
38+
{
39+
directive: DaffColorableDirective,
40+
inputs: ['color'],
41+
},
42+
],
43+
})
44+
export class CustomComponent { }
45+
```
46+
47+
### Setting a default color
48+
49+
Set `defaultColor` to apply a color when `color` is not explicitly provided:
50+
51+
```ts
52+
constructor(private colorable: DaffColorableDirective) {
53+
this.colorable.defaultColor = 'primary';
54+
}
55+
```
56+
57+
## Styling
58+
59+
Use the applied CSS class to style each color variant:
60+
61+
```scss
62+
.custom-component {
63+
&.daff-primary {
64+
background: theme.daff-color($primary, 10);
65+
color: daff-color($primary, 90);
66+
}
67+
}
68+
```
69+
70+
## Warnings
71+
72+
A console warning is shown in dev mode if a color is not part of the supported colors.

libs/design/src/core/colorable/colorable.directive.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { By } from '@angular/platform-browser';
1111

1212
import {
1313
DaffColorableDirective,
14-
DaffPalette,
14+
DaffColor,
1515
} from '@daffodil/design';
1616

1717
@Component({
@@ -23,7 +23,7 @@ import {
2323
})
2424

2525
class WrapperComponent {
26-
color: DaffPalette;
26+
color: DaffColor;
2727
}
2828

2929
describe('@daffodil/design | DaffColorableDirective', () => {

libs/design/src/core/colorable/colorable.directive.ts

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,83 +8,23 @@ import {
88
} from '@angular/core';
99

1010
import {
11-
DaffColorable,
12-
DaffPalette,
13-
DaffPaletteEnum,
11+
DaffColor,
12+
DaffColorEnum,
1413
} from './colorable';
1514

16-
const colorInPalette = (color: string) => (<any>Object).values(DaffPaletteEnum).includes(color);
15+
const isDaffColor = (color: string) => (<any>Object).values(DaffColorEnum).includes(color);
1716

1817
const validateColor = (color: string) => {
1918
if(isDevMode()) {
20-
if(color !== undefined && !colorInPalette(color)) {
21-
console.warn(color + ' is not a valid color in DaffPalette');
19+
if(color !== undefined && !isDaffColor(color)) {
20+
console.warn(color + ' is not a valid color in DaffColor');
2221
}
2322
}
2423
};
2524

2625
/**
27-
* `DaffColorableDirective` allows a component to conditionally apply color-specific
28-
* styles by setting CSS classes based on the specified color. This directive is useful
29-
* for applying different color palettes to a component in an Angular application.
30-
*
31-
* Supported colors: `primary | secondary | tertiary | light | dark | theme | theme-contrast`
32-
*
33-
* | Color | Class |
34-
* | -------- | ----- |
35-
* | `primary` | `.daff-primary`|
36-
* | `secondary` | `.daff-secondary`|
37-
* | `tertiary` | `.daff-tertiary`|
38-
* | `light` | `daff-light` |
39-
* | `dark` | `daff-dark` |
40-
* | `theme` | `daff-theme`|
41-
* | `theme-contrast` | `.daff-theme-contrast`|
42-
*
43-
* `white` and `black` have been deprecated in favor of `light` and `dark`.
44-
*
45-
* @example Implementing it as an attribute directive
46-
*
47-
* ```html
48-
* <div daffColorable [color]="primary">Colored content</div>
49-
* ```
50-
*
51-
* ```scss
52-
* .div {
53-
* &.daff-primary {
54-
* color: daff-color($primary);
55-
* }
56-
* }
57-
* ```
58-
*
59-
* In this example, the `daff-primary` class is applied to the `div` element, allowing you to
60-
* use the color class to style the `div`.
61-
*
62-
* @example Implementing it as an Angular host directive
63-
*
64-
* ```ts
65-
* @Component({
66-
* selector: 'custom-component',
67-
* template: 'custom-component.html',
68-
* hostDirectives: [
69-
* {
70-
* directive: DaffColorableDirective,
71-
* inputs: ['color'],
72-
* },
73-
* ],
74-
* })
75-
* export class CustomComponent {
76-
* @HostBinding('class.custom-component') class = true;
77-
* }
78-
* ```
79-
*
80-
* ```scss
81-
* .custom-component {
82-
* &.daff-primary {
83-
* background: daff-color($primary, 10);
84-
* color: daff-color($primary, 90);
85-
* }
86-
* }
87-
* ```
26+
* Enforces consistent use of {@link DaffColor} on a component by applying
27+
* color-specific CSS classes and validating the color in dev mode.
8828
*/
8929
@Directive({
9030
selector: '[daffColorable]',
@@ -100,23 +40,16 @@ const validateColor = (color: string) => {
10040
'[class.daff-white]': 'color === "white"',
10141
},
10242
})
103-
export class DaffColorableDirective implements DaffColorable, OnChanges, OnInit {
43+
export class DaffColorableDirective implements OnChanges, OnInit {
10444
/**
105-
* Sets the color on a component.
45+
* The color of the component.
10646
*/
107-
@Input() color: DaffPalette;
47+
@Input() color: DaffColor;
10848

10949
/**
110-
* Sets a default color.
111-
*
112-
* @example
113-
* ```ts
114-
* constructor(private colorableDirective: DaffColorableDirective) {
115-
* this.colorableDirective.defaultColor = 'theme';
116-
* }
117-
* ```
50+
* The default color used when no color is set.
11851
*/
119-
defaultColor: DaffPalette;
52+
defaultColor: DaffColor;
12053

12154
/**
12255
* @docs-private
Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,66 @@
1+
/**
2+
* @deprecated Deprecated in version 0.92.0. Will be removed in version 1.0.0.
3+
*/
14
export interface DaffColorable {
2-
color: DaffPalette;
5+
color: DaffColor;
36
}
47

58
/**
6-
* These are the valid options that can be passed to a DaffColorable component.
9+
* The available color options.
10+
*/
11+
export type DaffColor = 'primary' | 'secondary' | 'tertiary' | 'light' | 'dark' | 'theme' | 'theme-contrast' | 'black' | 'white' | undefined;
12+
13+
/**
14+
* @deprecated Deprecated in version 0.92.0. Will be removed in version 1.0.0.
715
*/
8-
export type DaffPalette = 'primary' | 'secondary' | 'tertiary' | 'light' | 'dark' | 'theme' | 'theme-contrast' | 'black' | 'white' | undefined;
16+
export type DaffPalette = DaffColor;
917

1018
/**
11-
* Enumerates the available color palette options for a component.
12-
* These values can be used to apply specific color styles to components within the application.
19+
* The available color options.
1320
*/
14-
export enum DaffPaletteEnum {
21+
export enum DaffColorEnum {
1522
/**
16-
* Your primary color.
23+
* The primary color.
1724
*/
1825
Primary = 'primary',
1926

2027
/**
21-
* Your secondary color.
28+
* The secondary color.
2229
*/
2330
Secondary = 'secondary',
2431

2532
/**
26-
* Your tertiary color.
33+
* The tertiary color.
2734
*/
2835
Tertiary = 'tertiary',
2936

3037
/**
31-
* A light color that does not change based on the defined theme.
38+
* A light color that does not change based on the theme.
3239
*/
3340
Light = 'light',
3441

3542
/**
36-
* A dark color that does not change based on the defined theme.
43+
* A dark color that does not change based on the theme.
3744
*/
3845
Dark = 'dark',
3946

4047
/**
41-
* A color that matches the defined theme.
48+
* A color that matches the theme.
4249
*/
4350
Theme = 'theme',
4451

4552
/**
46-
* A color that contrasts against the defined theme.
53+
* A color that contrasts against the theme.
4754
*/
4855
ThemeContrast = 'theme-contrast',
4956

5057
/**
5158
* @deprecated Deprecated in version 0.82.0. Will be removed in version 1.0.0.
52-
* Black. It's dark.
5359
*/
5460
Black = 'black',
5561

5662
/**
5763
* @deprecated Deprecated in version 0.82.0. Will be removed in version 1.0.0.
58-
* White. It's bright.
5964
*/
6065
White = 'white',
6166
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export {
22
DaffPalette,
3-
DaffColorable,
3+
DaffColor,
44
} from './colorable';
5+
export { DaffColorable } from './colorable';
56
export { DaffColorableDirective } from './colorable.directive';

0 commit comments

Comments
 (0)