|
| 1 | +import { makeResetStyles, makeStyles, mergeClasses } from '@griffel/react'; |
| 2 | +import { type FieldState, fieldClassNames } from '@fluentui/react-field'; |
| 3 | +import { getSlotClassNameProp_unstable } from '@fluentui/react-utilities'; |
| 4 | +import * as semanticTokens from '@fluentui/semantic-tokens'; |
| 5 | + |
| 6 | +// Size of the icon in the validation message |
| 7 | +const iconSize = '12px'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Styles for the root slot |
| 11 | + */ |
| 12 | +const useRootStyles = makeStyles({ |
| 13 | + base: { |
| 14 | + display: 'grid', |
| 15 | + }, |
| 16 | + |
| 17 | + // In horizontal layout, the field is a grid with the label taking up the entire first column. |
| 18 | + // The last row is slack space in case the label is taller than the rest of the content. |
| 19 | + horizontal: { |
| 20 | + gridTemplateColumns: '33% 1fr', |
| 21 | + gridTemplateRows: 'auto auto auto 1fr', |
| 22 | + }, |
| 23 | + |
| 24 | + // In horizontal layout without a label, replace the label's column with padding. |
| 25 | + // This lets grid auto-flow properly place the other children, and keeps fields with and without labels aligned. |
| 26 | + horizontalNoLabel: { |
| 27 | + paddingLeft: '33%', |
| 28 | + gridTemplateColumns: '1fr', |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +const useLabelStyles = makeStyles({ |
| 33 | + vertical: { |
| 34 | + paddingTop: semanticTokens._ctrlFieldPaddingCtrlTextTop, |
| 35 | + paddingBottom: semanticTokens._ctrlFieldPaddingCtrlTextBottom, |
| 36 | + marginBottom: semanticTokens.gapBetweenContentXSmall, |
| 37 | + }, |
| 38 | + |
| 39 | + verticalLarge: { |
| 40 | + paddingTop: semanticTokens._ctrlFieldPaddingCtrlLgTextTop, |
| 41 | + paddingBottom: semanticTokens._ctrlFieldPaddingCtrlLgTextBottom, |
| 42 | + marginBottom: semanticTokens._ctrlFieldGapBetweenContentXSmall, |
| 43 | + }, |
| 44 | + |
| 45 | + horizontal: { |
| 46 | + paddingTop: semanticTokens._ctrlFieldPaddingCtrlTextTopHorizontal, |
| 47 | + paddingBottom: semanticTokens._ctrlFieldPaddingCtrlTextBottomHorizontal, |
| 48 | + marginRight: semanticTokens._ctrlFieldPaddingCtrlTextSide, |
| 49 | + gridRowStart: '1', |
| 50 | + gridRowEnd: '-1', |
| 51 | + }, |
| 52 | + |
| 53 | + horizontalSmall: { |
| 54 | + paddingTop: semanticTokens._ctrlFieldPaddingCtrlSmTextTop, |
| 55 | + paddingBottom: semanticTokens._ctrlFieldPaddingCtrlSmTextBottom, |
| 56 | + }, |
| 57 | + |
| 58 | + horizontalLarge: { |
| 59 | + // To align the label text with the Input text, it should be centered within the 40px height of the Input. |
| 60 | + // This is (40px - lineHeightBase400) / 2 = 9px. Hardcoded since there is no 9px padding token. |
| 61 | + paddingTop: semanticTokens._ctrlFieldPaddingCtrlLgTextTopHorizontal, |
| 62 | + paddingBottom: semanticTokens._ctrlFieldPaddingCtrlLgTextBottomHorizontal, |
| 63 | + }, |
| 64 | +}); |
| 65 | + |
| 66 | +const useSecondaryTextBaseClassName = makeResetStyles({ |
| 67 | + marginTop: semanticTokens.gapBetweenContentXSmall, |
| 68 | + color: semanticTokens._ctrlFieldForegroundCtrlNeutralSecondaryRest, |
| 69 | + fontFamily: semanticTokens.textStyleDefaultRegularFontFamily, |
| 70 | + fontWeight: semanticTokens.textStyleDefaultRegularWeight, |
| 71 | + fontSize: semanticTokens.textRampMetadataFontSize, |
| 72 | + lineHeight: semanticTokens.textRampMetadataLineHeight, |
| 73 | +}); |
| 74 | + |
| 75 | +const useSecondaryTextStyles = makeStyles({ |
| 76 | + error: { |
| 77 | + color: semanticTokens._ctrlFieldStatusDangerTintForeground, |
| 78 | + }, |
| 79 | + |
| 80 | + withIcon: { |
| 81 | + // Add a gutter for the icon, to allow multiple lines of text to line up to the right of the icon. |
| 82 | + paddingLeft: `calc(${iconSize} + ${semanticTokens.gapBetweenTextSmall})`, |
| 83 | + }, |
| 84 | +}); |
| 85 | + |
| 86 | +const useValidationMessageIconBaseClassName = makeResetStyles({ |
| 87 | + display: 'inline-block', |
| 88 | + fontSize: iconSize, |
| 89 | + // Negative left margin puts the icon in the gutter of the validation message div's withIcon style. |
| 90 | + marginLeft: `calc(-${iconSize} - ${semanticTokens.gapBetweenTextSmall})`, |
| 91 | + marginRight: semanticTokens.gapBetweenTextSmall, |
| 92 | + // Line height of 0 prevents the verticalAlign from affecting the line height of the text. |
| 93 | + lineHeight: '0', |
| 94 | + // Negative verticalAlign shifts the inline icon down to align with the text (effectively top padding). |
| 95 | + verticalAlign: '-1px', |
| 96 | +}); |
| 97 | + |
| 98 | +const useValidationMessageIconStyles = makeStyles({ |
| 99 | + error: { |
| 100 | + color: semanticTokens._ctrlFieldStatusDangerTintForeground, |
| 101 | + }, |
| 102 | + warning: { |
| 103 | + color: semanticTokens._ctrlFieldStatusWarningTintForeground, |
| 104 | + }, |
| 105 | + success: { |
| 106 | + color: semanticTokens._ctrlFieldStatusSuccessTintForeground, |
| 107 | + }, |
| 108 | +}); |
| 109 | + |
| 110 | +/** |
| 111 | + * Apply styling to the Field slots based on the state |
| 112 | + */ |
| 113 | +export const useSemanticFieldStyles = (_state: unknown): FieldState => { |
| 114 | + 'use no memo'; |
| 115 | + |
| 116 | + const state = _state as FieldState; |
| 117 | + const { validationState, size } = state; |
| 118 | + const horizontal = state.orientation === 'horizontal'; |
| 119 | + |
| 120 | + const rootStyles = useRootStyles(); |
| 121 | + state.root.className = mergeClasses( |
| 122 | + state.root.className, |
| 123 | + fieldClassNames.root, |
| 124 | + rootStyles.base, |
| 125 | + horizontal && rootStyles.horizontal, |
| 126 | + horizontal && !state.label && rootStyles.horizontalNoLabel, |
| 127 | + getSlotClassNameProp_unstable(state.root), |
| 128 | + ); |
| 129 | + |
| 130 | + const labelStyles = useLabelStyles(); |
| 131 | + if (state.label) { |
| 132 | + state.label.className = mergeClasses( |
| 133 | + state.label.className, |
| 134 | + fieldClassNames.label, |
| 135 | + horizontal && labelStyles.horizontal, |
| 136 | + horizontal && size === 'small' && labelStyles.horizontalSmall, |
| 137 | + horizontal && size === 'large' && labelStyles.horizontalLarge, |
| 138 | + !horizontal && labelStyles.vertical, |
| 139 | + !horizontal && size === 'large' && labelStyles.verticalLarge, |
| 140 | + getSlotClassNameProp_unstable(state.label), |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + const validationMessageIconBaseClassName = useValidationMessageIconBaseClassName(); |
| 145 | + const validationMessageIconStyles = useValidationMessageIconStyles(); |
| 146 | + if (state.validationMessageIcon) { |
| 147 | + state.validationMessageIcon.className = mergeClasses( |
| 148 | + state.validationMessageIcon.className, |
| 149 | + fieldClassNames.validationMessageIcon, |
| 150 | + validationMessageIconBaseClassName, |
| 151 | + validationState !== 'none' && validationMessageIconStyles[validationState], |
| 152 | + getSlotClassNameProp_unstable(state.validationMessageIcon), |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + const secondaryTextBaseClassName = useSecondaryTextBaseClassName(); |
| 157 | + const secondaryTextStyles = useSecondaryTextStyles(); |
| 158 | + if (state.validationMessage) { |
| 159 | + state.validationMessage.className = mergeClasses( |
| 160 | + state.validationMessage.className, |
| 161 | + fieldClassNames.validationMessage, |
| 162 | + secondaryTextBaseClassName, |
| 163 | + validationState === 'error' && secondaryTextStyles.error, |
| 164 | + !!state.validationMessageIcon && secondaryTextStyles.withIcon, |
| 165 | + getSlotClassNameProp_unstable(state.validationMessage), |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + if (state.hint) { |
| 170 | + state.hint.className = mergeClasses( |
| 171 | + state.hint.className, |
| 172 | + fieldClassNames.hint, |
| 173 | + secondaryTextBaseClassName, |
| 174 | + getSlotClassNameProp_unstable(state.hint), |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + return state; |
| 179 | +}; |
0 commit comments