@workday/canvas-kit-docs 8.4.0 → 8.4.2

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.
@@ -0,0 +1,14 @@
1
+ import Template from './examples/compoundComponent/CustomCard';
2
+
3
+
4
+ # Canvas Kit Examples
5
+
6
+ ## Compound Component
7
+
8
+ Component component utilites are a great way to wrap and extend exisitng Canvas Kit Components. This
9
+ can dramatically simplify state handling and gives you easy access to things like ref forwarding.
10
+
11
+ For more complex examples,
12
+ [see the creating-compound-components guide](https://workday.github.io/canvas-kit/?path=/docs/guides-creating-compound-components--page).
13
+
14
+ <ExampleCodeBlock code={Template} />
@@ -0,0 +1,125 @@
1
+ import React from 'react';
2
+ import {
3
+ createContainer,
4
+ createElemPropsHook,
5
+ createModelHook,
6
+ createSubcomponent,
7
+ ExtractProps,
8
+ } from '@workday/canvas-kit-react/common';
9
+ import {Card} from '@workday/canvas-kit-react/card';
10
+ import {PrimaryButton} from '@workday/canvas-kit-react/button';
11
+ import {Box} from '@workday/canvas-kit-react/layout';
12
+ import {colors} from '@workday/canvas-kit-react/tokens';
13
+
14
+ export type Theme = 'dark' | 'light';
15
+ interface CustomCardProps extends ExtractProps<typeof Card> {
16
+ theme?: Theme;
17
+ }
18
+
19
+ /**
20
+ * Compound Component Model Hook
21
+ */
22
+ const useCustomCardModel = createModelHook({
23
+ defaultConfig: {theme: 'dark' as Theme},
24
+ })(config => {
25
+ const lightThemeColor = colors.frenchVanilla100;
26
+ const darkThemeColor = colors.blackPepper300;
27
+ const inverseColor = color => (color === darkThemeColor ? lightThemeColor : darkThemeColor);
28
+
29
+ const [theme, setTheme] = React.useState(config.theme);
30
+ const [mainColor, setMainColor] = React.useState(
31
+ config.theme === 'dark' ? darkThemeColor : lightThemeColor
32
+ );
33
+ const [contrastColor, setContrastColor] = React.useState(
34
+ config.theme === 'dark' ? lightThemeColor : darkThemeColor
35
+ );
36
+ const state = {theme, mainColor, contrastColor};
37
+
38
+ const events = {
39
+ toggleTheme: () => {
40
+ setTheme(currentTheme => (currentTheme === 'dark' ? 'light' : 'dark'));
41
+ setMainColor(inverseColor);
42
+ setContrastColor(inverseColor);
43
+ },
44
+ };
45
+
46
+ return {state, events};
47
+ });
48
+
49
+ /**
50
+ * Subcomponent Hook
51
+ */
52
+ const useCustomCardButton = createElemPropsHook(useCustomCardModel)(({state, events}) => ({
53
+ colors: {
54
+ default: {
55
+ background: state.contrastColor,
56
+ label: state.mainColor,
57
+ },
58
+ hover: {},
59
+ active: {},
60
+ focus: {},
61
+ disabled: {},
62
+ },
63
+ onClick: () => events.toggleTheme(),
64
+ }));
65
+
66
+ /**
67
+ * Subcomponent Component
68
+ */
69
+ const CustomCardButton = createSubcomponent(PrimaryButton)({
70
+ displayName: 'CustomCard.Button',
71
+ modelHook: useCustomCardModel,
72
+ elemPropsHook: useCustomCardButton,
73
+ })(({children, ...elemProps}) => {
74
+ return <PrimaryButton {...elemProps}>{children}</PrimaryButton>;
75
+ });
76
+
77
+ /**
78
+ * Main Component Hook
79
+ */
80
+ const useCustomCard = createElemPropsHook(useCustomCardModel)(({state}) => ({
81
+ backgroundColor: state.mainColor,
82
+ }));
83
+
84
+ /**
85
+ * Main Component
86
+ */
87
+ const CustomCard = createContainer(Card)({
88
+ displayName: 'CustomCard',
89
+ subComponents: {
90
+ Heading: Card.Heading,
91
+ Body: Card.Body,
92
+ Button: CustomCardButton,
93
+ },
94
+ modelHook: useCustomCardModel,
95
+ elemPropsHook: useCustomCard,
96
+ })<CustomCardProps>(({theme, children, ...elemProps}) => {
97
+ return (
98
+ <Card depth={2} {...elemProps}>
99
+ {children}
100
+ </Card>
101
+ );
102
+ });
103
+
104
+ /**
105
+ * Usage
106
+ */
107
+ export default () => {
108
+ const [icon, setIcon] = React.useState('🌝');
109
+ const model = useCustomCardModel({
110
+ onToggleTheme: (_, {theme: previousTheme}) => setIcon(previousTheme === 'dark' ? '🌚' : '🌝'),
111
+ });
112
+ const buttonRef = React.useRef<HTMLButtonElement>(null);
113
+ React.useEffect(() => buttonRef.current?.focus(), []);
114
+
115
+ return (
116
+ <CustomCard model={model}>
117
+ <CustomCard.Heading>
118
+ <Box color={model.state.contrastColor}>Change your theme {icon}</Box>
119
+ </CustomCard.Heading>
120
+ <CustomCard.Body>
121
+ <CustomCard.Button ref={buttonRef}>Toggle</CustomCard.Button>
122
+ </CustomCard.Body>
123
+ </CustomCard>
124
+ );
125
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workday/canvas-kit-docs",
3
- "version": "8.4.0",
3
+ "version": "8.4.2",
4
4
  "description": "Documentation components of Canvas Kit components",
5
5
  "author": "Workday, Inc. (https://www.workday.com)",
6
6
  "license": "Apache-2.0",
@@ -42,7 +42,7 @@
42
42
  ],
43
43
  "dependencies": {
44
44
  "@storybook/csf": "0.0.1",
45
- "@workday/canvas-kit-react": "^8.4.0",
45
+ "@workday/canvas-kit-react": "^8.4.2",
46
46
  "@workday/canvas-system-icons-web": "^3.0.0"
47
47
  },
48
48
  "devDependencies": {
@@ -51,5 +51,5 @@
51
51
  "mkdirp": "^1.0.3",
52
52
  "typescript": "4.1"
53
53
  },
54
- "gitHead": "7ec0c1d1a2e4df3a0febba0c481063b25c1c5dc1"
54
+ "gitHead": "a9db88be010de89d2ecb4380c05ce3f62478de26"
55
55
  }