Skip to content

Commit 5738fa9

Browse files
committed
Tweak the Docs and code for new variant prop
1 parent ba175ca commit 5738fa9

File tree

10 files changed

+63
-30
lines changed

10 files changed

+63
-30
lines changed

README.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ Check out these demos:
3838
- [`isInvalid`](#isinvalid--default-false)
3939
- [`focusBorderColor` / `errorBorderColor`](#focusbordercolor--default-blue500--errorbordercolor--default-red500)
4040
- [`useBasicStyles`](#usebasicstyles--default-false)
41-
- [`variant`](#variant)
4241
- [`selectedOptionStyle`](#selectedoptionstyle--options-color--check--default-color)
4342
- [`selectedOptionColor`](#selectedoptioncolor--default-blue)
43+
- [`variant`](#variant--options-outline--filled--flushed--unstyled--default-outline)
4444
- [`hasStickyGroupHeaders`](#hasstickygroupheaders--default-false)
4545
- [`isFixed`](#isfixed)
4646
- [Styling](#styling)
@@ -252,16 +252,6 @@ return (
252252

253253
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-border-selectedoptionstyle-yxkcos?file=/example.js)
254254

255-
#### `variant`
256-
257-
You can set `outline`, `unstyled`, `flushed` or `filled`, which will reflect the same styles of Chakra-Ui select possible appearances [Check chakra-ui variants styles](https://chakra-ui.com/docs/components/select#changing-the-appearance)!
258-
259-
```js
260-
return (
261-
<Select variant="flushed" />
262-
);
263-
```
264-
265255
#### `selectedOptionColor` — Default: `blue`
266256

267257
If you choose to stick with the default `selectedOptionStyle="color"`, you have one additional styling option. If you do not like the default of blue for the highlight color, you can pass the `selectedOptionColor` prop to change it. This prop will accept any named color from your color theme, and it will use the `500` value in light mode or the `300` value in dark mode.
@@ -281,6 +271,35 @@ return (
281271

282272
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-border-selectedoptioncolor-yyd321?file=/example.js)
283273

274+
#### `variant` — Options: `outline` | `filled` | `flushed` | `unstyled` — Default: `outline`
275+
276+
You can pass the `variant` prop with any of `outline`, `filled`, `flushed`, or `unstyled` to change the overall styling of the `Select`. These will reflect the various appearances available for [Chakra's `<Input />` component](https://chakra-ui.com/docs/components/input#changing-the-size-of-the-input).
277+
278+
```js
279+
return (
280+
<>
281+
<Select variant="outline" /> {/* Default */}
282+
<Select variant="filled" />
283+
<Select variant="flushed" />
284+
<Select variant="unstyled" />
285+
</>
286+
);
287+
```
288+
289+
![variant in light mode](./github/variant-light.png)
290+
291+
![variant in dark mode](./github/variant-dark.png)
292+
293+
By default, the `flushed` and `unstyled` variants look a bit strange in combination with the `DropdownIndicator`. An easy way to make these styles look more natural is to pass the [`useBasicStyles`](#usebasicstyles--default-false) prop along with them to remove the background from the indicator. Or alternatively, you could hide the indicator completely using [`chakraStyles`](#chakrastyles).
294+
295+
![variant with useBasicStyles](./github/variant-use-basic-styles.png)
296+
297+
Another thing to note is that the default styling for `variant="filled"` and `isMulti` results in the select and selected option tags having the same background color when the select is not focused. The easiest solution for this is to pass the [`tagVariant`](#tagvariant--options-subtle--solid--outline--default-subtle) or [`colorScheme`](#colorscheme) prop to add some contrast between the two elements.
298+
299+
![variant with useBasicStyles](./github/filled-variant.png)
300+
301+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-variant-5cf755?file=/example.js)
302+
284303
#### `hasStickyGroupHeaders` — Default: `false`
285304

286305
One additional feature which isn’t specific to Chakra or react-select is sticky group headers. It adds a border to the bottom of the header and keeps it in view while its corresponding group of options is visible. This can be very nice for when you have long lists of grouped options so you can always tell which group of options you're looking at. To add it, pass the `hasStickyGroupHeaders` prop to the select component.

github/filled-variant.png

128 KB
Loading

github/variant-dark.png

128 KB
Loading

github/variant-light.png

57 KB
Loading
85.8 KB
Loading

src/chakra-components/containers.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ export const ValueContainer = <
7373
} = props;
7474

7575
// Getting the css from input instead of select
76-
// to fit better with the search input feature
77-
78-
const chakraInputConfig = useMultiStyleConfig("Input", {
76+
// to fit better with each of the variants
77+
const inputStyles = useMultiStyleConfig("Input", {
7978
size,
8079
variant,
8180
});
@@ -84,7 +83,8 @@ export const ValueContainer = <
8483
display: "flex",
8584
alignItems: "center",
8685
flex: 1,
87-
px: chakraInputConfig.field.px,
86+
paddingY: "2px",
87+
paddingX: inputStyles.field.px,
8888
flexWrap: "wrap",
8989
WebkitOverflowScrolling: "touch",
9090
position: "relative",

src/chakra-components/control.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ export const IndicatorSeparator = <
102102
const {
103103
className,
104104
cx,
105-
selectProps: { chakraStyles, useBasicStyles },
105+
selectProps: { chakraStyles, useBasicStyles, variant },
106106
} = props;
107107

108108
const initialSx: CSSObject = {
109109
opacity: 1,
110-
...(useBasicStyles && { display: "none" }),
110+
...(useBasicStyles || variant !== "outline" ? { display: "none" } : {}),
111111
};
112112

113113
const sx = chakraStyles?.indicatorSeparator

src/module-augmentation.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,6 @@ declare module "react-select/dist/declarations/src/Select" {
7676
*/
7777
colorScheme?: string;
7878

79-
/**
80-
* The `variant` prop that changes the select main style
81-
*
82-
* Options: "outline" | "unstyled" | "flushed" | "filled"
83-
*
84-
* @defaultValue `outline`
85-
* @see {@link https://chakra-ui.com/docs/components/select#changing-the-appearance}
86-
*/
87-
variant?: Variant;
88-
8979
/**
9080
* The `variant` prop that will be forwarded to your `MultiValue` component
9181
* which is represented by a chakra `Tag`
@@ -168,6 +158,17 @@ declare module "react-select/dist/declarations/src/Select" {
168158
* @see {@link https://chakra-ui.com/docs/components/select}
169159
*/
170160
useBasicStyles?: boolean;
161+
162+
/**
163+
* The main style variant of the `Select` component
164+
*
165+
* Options: `outline` | `filled` | `flushed` | `unstyled`
166+
*
167+
* @defaultValue `outline`
168+
* @see {@link https://chakra-ui.com/docs/components/select#changing-the-appearance}
169+
* @see {@link https://github.com/csandman/chakra-react-select#variant--options-outline--filled--flushed--unstyled--default-outline}
170+
*/
171+
variant?: Variant;
171172
}
172173
}
173174

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ export interface SizeProps<PropType = string | number> {
4040

4141
export type Size = "sm" | "md" | "lg";
4242

43-
export type Variant = "outline" | "unstyled" | "flushed" | "filled";
44-
4543
export type TagVariant = "subtle" | "solid" | "outline";
4644

4745
export type SelectedOptionStyle = "color" | "check";
4846

47+
export type Variant = "outline" | "filled" | "flushed" | "unstyled";
48+
4949
export type StylesFunction<ComponentProps> = (
5050
provided: CSSObject,
5151
state: ComponentProps

src/use-chakra-select-props.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useFormControl } from "@chakra-ui/form-control";
22
import type { GroupBase, Props } from "react-select";
33
import chakraComponents from "./chakra-components";
4-
import type { SelectedOptionStyle, Size, TagVariant } from "./types";
4+
import type { SelectedOptionStyle, Size, TagVariant, Variant } from "./types";
55

66
const useChakraSelectProps = <
77
Option,
@@ -21,6 +21,7 @@ const useChakraSelectProps = <
2121
hasStickyGroupHeaders = false,
2222
selectedOptionStyle = "color",
2323
selectedOptionColor = "blue",
24+
variant = "outline",
2425
focusBorderColor,
2526
errorBorderColor,
2627
chakraStyles = {},
@@ -75,6 +76,17 @@ const useChakraSelectProps = <
7576
realSelectedOptionColor = "blue";
7677
}
7778

79+
let realVariant: Variant = variant;
80+
const variantOptions: Variant[] = [
81+
"outline",
82+
"filled",
83+
"flushed",
84+
"unstyled",
85+
];
86+
if (!variantOptions.includes(variant)) {
87+
realVariant = "outline";
88+
}
89+
7890
const select: Props<Option, IsMulti, Group> = {
7991
// Allow overriding of custom components
8092
components: {
@@ -87,6 +99,7 @@ const useChakraSelectProps = <
8799
tagVariant: realTagVariant,
88100
selectedOptionStyle: realSelectedOptionStyle,
89101
selectedOptionColor: realSelectedOptionColor,
102+
variant: realVariant,
90103
hasStickyGroupHeaders,
91104
chakraStyles,
92105
focusBorderColor,

0 commit comments

Comments
 (0)