|
| 1 | +import { makeResetStyles, makeStyles, mergeClasses } from '@griffel/react'; |
| 2 | +import { getSlotClassNameProp_unstable } from '@fluentui/react-utilities'; |
| 3 | +import { tokens } from '@fluentui/react-theme'; |
| 4 | +import { useSemanticInputStyles } from '../Input'; |
| 5 | +import { type SearchBoxState, searchBoxClassNames } from '@fluentui/react-search'; |
| 6 | +import * as semanticTokens from '@fluentui/semantic-tokens'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Styles for the root slot |
| 10 | + */ |
| 11 | +const useRootStyles = makeStyles({ |
| 12 | + small: { |
| 13 | + columnGap: 0, |
| 14 | + maxWidth: '468px', |
| 15 | + |
| 16 | + paddingLeft: tokens.spacingHorizontalSNudge, |
| 17 | + paddingRight: tokens.spacingHorizontalSNudge, |
| 18 | + }, |
| 19 | + medium: { |
| 20 | + columnGap: 0, |
| 21 | + maxWidth: '468px', |
| 22 | + |
| 23 | + paddingLeft: tokens.spacingHorizontalS, |
| 24 | + paddingRight: tokens.spacingHorizontalS, |
| 25 | + }, |
| 26 | + large: { |
| 27 | + columnGap: 0, |
| 28 | + maxWidth: '468px', |
| 29 | + |
| 30 | + paddingLeft: tokens.spacingHorizontalMNudge, |
| 31 | + paddingRight: tokens.spacingHorizontalMNudge, |
| 32 | + }, |
| 33 | + |
| 34 | + input: { |
| 35 | + paddingLeft: tokens.spacingHorizontalSNudge, |
| 36 | + paddingRight: 0, |
| 37 | + |
| 38 | + // removes the WebKit pseudoelement styling |
| 39 | + '::-webkit-search-decoration': { |
| 40 | + display: 'none', |
| 41 | + }, |
| 42 | + '::-webkit-search-cancel-button': { |
| 43 | + display: 'none', |
| 44 | + }, |
| 45 | + }, |
| 46 | + |
| 47 | + unfocusedNoContentAfter: { |
| 48 | + paddingRight: 0, |
| 49 | + }, |
| 50 | +}); |
| 51 | + |
| 52 | +const useInputStyles = makeStyles({ |
| 53 | + small: { |
| 54 | + paddingRight: tokens.spacingHorizontalSNudge, |
| 55 | + }, |
| 56 | + medium: { |
| 57 | + paddingRight: tokens.spacingHorizontalS, |
| 58 | + }, |
| 59 | + large: { |
| 60 | + paddingRight: tokens.spacingHorizontalMNudge, |
| 61 | + }, |
| 62 | +}); |
| 63 | + |
| 64 | +const useContentAfterStyles = makeStyles({ |
| 65 | + contentAfter: { |
| 66 | + paddingLeft: tokens.spacingHorizontalM, |
| 67 | + columnGap: tokens.spacingHorizontalXS, |
| 68 | + }, |
| 69 | + rest: { |
| 70 | + height: 0, |
| 71 | + width: 0, |
| 72 | + paddingLeft: 0, |
| 73 | + overflow: 'hidden', |
| 74 | + }, |
| 75 | +}); |
| 76 | + |
| 77 | +const useDismissClassName = makeResetStyles({ |
| 78 | + boxSizing: 'border-box', |
| 79 | + color: semanticTokens.foregroundCtrlIconOnNeutralRest, // "icon color" in design spec |
| 80 | + display: 'flex', |
| 81 | + // special case styling for icons (most common case) to ensure they're centered vertically |
| 82 | + // size: medium (default) |
| 83 | + '> svg': { fontSize: '20px' }, |
| 84 | + cursor: 'pointer', |
| 85 | +}); |
| 86 | + |
| 87 | +const useDismissStyles = makeStyles({ |
| 88 | + disabled: { |
| 89 | + color: semanticTokens.backgroundCtrlSubtleRest, |
| 90 | + }, |
| 91 | + // Ensure resizable icons show up with the proper font size |
| 92 | + small: { |
| 93 | + '> svg': { fontSize: '16px' }, |
| 94 | + }, |
| 95 | + medium: { |
| 96 | + // included in useDismissClassName |
| 97 | + }, |
| 98 | + large: { |
| 99 | + '> svg': { fontSize: '24px' }, |
| 100 | + }, |
| 101 | +}); |
| 102 | + |
| 103 | +/** |
| 104 | + * Apply styling to the SearchBox slots based on the state |
| 105 | + */ |
| 106 | +export const useSemanticSearchBoxStyles = (_state: unknown): SearchBoxState => { |
| 107 | + 'use no memo'; |
| 108 | + |
| 109 | + const state = _state as SearchBoxState; |
| 110 | + |
| 111 | + const { disabled, focused, size } = state; |
| 112 | + |
| 113 | + const rootStyles = useRootStyles(); |
| 114 | + const inputStyles = useInputStyles(); |
| 115 | + const contentAfterStyles = useContentAfterStyles(); |
| 116 | + const dismissClassName = useDismissClassName(); |
| 117 | + const dismissStyles = useDismissStyles(); |
| 118 | + |
| 119 | + state.root.className = mergeClasses( |
| 120 | + state.root.className, |
| 121 | + searchBoxClassNames.root, |
| 122 | + rootStyles[size], |
| 123 | + !focused && rootStyles.unfocusedNoContentAfter, |
| 124 | + getSlotClassNameProp_unstable(state.root), |
| 125 | + ); |
| 126 | + |
| 127 | + state.input.className = mergeClasses( |
| 128 | + state.input.className, |
| 129 | + searchBoxClassNames.input, |
| 130 | + rootStyles.input, |
| 131 | + !focused && inputStyles[size], |
| 132 | + getSlotClassNameProp_unstable(state.input), |
| 133 | + ); |
| 134 | + |
| 135 | + if (state.dismiss) { |
| 136 | + state.dismiss.className = mergeClasses( |
| 137 | + state.dismiss.className, |
| 138 | + searchBoxClassNames.dismiss, |
| 139 | + dismissClassName, |
| 140 | + disabled && dismissStyles.disabled, |
| 141 | + dismissStyles[size], |
| 142 | + getSlotClassNameProp_unstable(state.dismiss), |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + if (state.contentBefore) { |
| 147 | + state.contentBefore.className = mergeClasses( |
| 148 | + state.contentBefore.className, |
| 149 | + searchBoxClassNames.contentBefore, |
| 150 | + getSlotClassNameProp_unstable(state.contentBefore), |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + if (state.contentAfter) { |
| 155 | + state.contentAfter.className = mergeClasses( |
| 156 | + state.contentAfter.className, |
| 157 | + searchBoxClassNames.contentAfter, |
| 158 | + contentAfterStyles.contentAfter, |
| 159 | + !focused && contentAfterStyles.rest, |
| 160 | + getSlotClassNameProp_unstable(state.contentAfter), |
| 161 | + ); |
| 162 | + } else if (state.dismiss) { |
| 163 | + state.dismiss.className = mergeClasses( |
| 164 | + contentAfterStyles.contentAfter, |
| 165 | + state.dismiss.className, |
| 166 | + getSlotClassNameProp_unstable(state.dismiss), |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + useSemanticInputStyles(state); |
| 171 | + |
| 172 | + return state; |
| 173 | +}; |
0 commit comments