|
| 1 | +import * as React from "react"; |
| 2 | + |
| 3 | +import {StyleSheet} from "aphrodite"; |
| 4 | +import {PropsFor, View} from "@khanacademy/wonder-blocks-core"; |
| 5 | +import {AllVariants} from "./all-variants"; |
| 6 | +import {semanticColor, sizing} from "@khanacademy/wonder-blocks-tokens"; |
| 7 | +import {LabelLarge, LabelMedium} from "@khanacademy/wonder-blocks-typography"; |
| 8 | + |
| 9 | +export const commonStates = { |
| 10 | + rest: {name: "Rest", className: "rest"}, |
| 11 | + hover: {name: "Hover", className: "hover"}, |
| 12 | + press: {name: "Press", className: "press"}, |
| 13 | + focus: {name: "Focus", className: "focusVisible"}, |
| 14 | + hoverAndFocus: {name: "Hover + Focus", className: "hover-focusVisible"}, |
| 15 | + pressAndFocus: {name: "Press + Focus", className: "press-focusVisible"}, |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * The default pseudo states for the `StateSheet` component. |
| 20 | + * |
| 21 | + * This is used to generate the pseudo states for the component in Chromatic. |
| 22 | + * Each className is a CSS class that is applied to the component to simulate |
| 23 | + * the pseudo state. |
| 24 | + * |
| 25 | + * @see https://github.com/chromaui/storybook-addon-pseudo-states?tab=readme-ov-file#targeting-specific-elements |
| 26 | + */ |
| 27 | +export const defaultPseudoStates = { |
| 28 | + hover: [ |
| 29 | + `.${commonStates.hover.className}`, |
| 30 | + `.${commonStates.hoverAndFocus.className}`, |
| 31 | + ], |
| 32 | + focusVisible: [ |
| 33 | + `.${commonStates.focus.className}`, |
| 34 | + `.${commonStates.hoverAndFocus.className}`, |
| 35 | + `.${commonStates.pressAndFocus.className}`, |
| 36 | + ], |
| 37 | + active: [ |
| 38 | + `.${commonStates.press.className}`, |
| 39 | + `.${commonStates.pressAndFocus.className}`, |
| 40 | + ], |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * The default states that will be included in the `StateSheet` component. |
| 45 | + */ |
| 46 | +export const defaultStates: Array<State> = Object.values(commonStates); |
| 47 | + |
| 48 | +/** |
| 49 | + * A state representing a pseudo state of a component. |
| 50 | + * e.g. `rest`, `hover`, `press`, `focus`, etc. |
| 51 | + */ |
| 52 | +type State = {name: string; className: string}; |
| 53 | + |
| 54 | +/** |
| 55 | + * The labels for the states presented in the row headers in the `StateSheet` |
| 56 | + * component. |
| 57 | + */ |
| 58 | +function StateLabels({ |
| 59 | + states, |
| 60 | + variant, |
| 61 | +}: { |
| 62 | + states: Array<State>; |
| 63 | + variant: string | React.ReactNode; |
| 64 | +}) { |
| 65 | + return ( |
| 66 | + <View style={styles.stateLabelsContainer}> |
| 67 | + <LabelLarge style={{alignSelf: "center"}}>{variant}</LabelLarge> |
| 68 | + <View |
| 69 | + style={[ |
| 70 | + styles.rowHeaderStates, |
| 71 | + {gridTemplateRows: `repeat(${states.length}, 40px)`}, |
| 72 | + ]} |
| 73 | + > |
| 74 | + {states.map(({name}, index) => ( |
| 75 | + <LabelMedium key={index}>{name}</LabelMedium> |
| 76 | + ))} |
| 77 | + </View> |
| 78 | + </View> |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +type Props = PropsFor<typeof AllVariants> & { |
| 83 | + /** |
| 84 | + * The states to display in the table as rows. |
| 85 | + */ |
| 86 | + states?: Array<State>; |
| 87 | +}; |
| 88 | + |
| 89 | +/** |
| 90 | + * A table that displays all possible states of a component. |
| 91 | + */ |
| 92 | +export function StateSheet({ |
| 93 | + children, |
| 94 | + columns, |
| 95 | + rows, |
| 96 | + states = defaultStates, |
| 97 | + title, |
| 98 | +}: Props) { |
| 99 | + // Override the default row headers to include the state labels. |
| 100 | + const rowsWithStateLabels = rows.map(({name, props}) => ({ |
| 101 | + name: <StateLabels variant={name} states={states} />, |
| 102 | + props, |
| 103 | + })); |
| 104 | + |
| 105 | + return ( |
| 106 | + <View> |
| 107 | + <AllVariants |
| 108 | + rows={rowsWithStateLabels} |
| 109 | + columns={columns} |
| 110 | + styles={{ |
| 111 | + rowHeader: styles.rowHeader, |
| 112 | + }} |
| 113 | + title={title} |
| 114 | + > |
| 115 | + {({props}) => { |
| 116 | + return ( |
| 117 | + <View style={styles.container}> |
| 118 | + {states.map(({className, name}) => |
| 119 | + children({ |
| 120 | + props, |
| 121 | + className, |
| 122 | + name, |
| 123 | + }), |
| 124 | + )} |
| 125 | + </View> |
| 126 | + ); |
| 127 | + }} |
| 128 | + </AllVariants> |
| 129 | + </View> |
| 130 | + ); |
| 131 | +} |
| 132 | + |
| 133 | +const styles = StyleSheet.create({ |
| 134 | + rowHeader: { |
| 135 | + verticalAlign: "top", |
| 136 | + padding: 0, |
| 137 | + }, |
| 138 | + container: { |
| 139 | + gap: sizing.size_160, |
| 140 | + }, |
| 141 | + |
| 142 | + stateLabelsContainer: { |
| 143 | + flexDirection: "row", |
| 144 | + gap: sizing.size_160, |
| 145 | + padding: sizing.size_160, |
| 146 | + justifyContent: "space-between", |
| 147 | + background: semanticColor.surface.secondary, |
| 148 | + borderTop: `${sizing.size_010} solid ${semanticColor.border.strong}`, |
| 149 | + }, |
| 150 | + rowHeaderStates: { |
| 151 | + gap: sizing.size_160, |
| 152 | + display: "grid", |
| 153 | + alignItems: "center", |
| 154 | + textAlign: "right", |
| 155 | + }, |
| 156 | +}); |
0 commit comments