Skip to content

Commit c8f7e23

Browse files
authored
Form component design tweaks (#2057)
* adds color-scheme style so inputs pick up UA color scheme styles * crops toggle switch knob's shadow inside the toggle switch boundaries * does not stretch FormControl input child to fill parent width unless 'block' prop is passed, and passes missing TextInputWrapper props in the Select component * adds changeset * rm duplicate prop
1 parent 9591843 commit c8f7e23

File tree

9 files changed

+27
-27
lines changed

9 files changed

+27
-27
lines changed

.changeset/tame-swans-rush.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
- adds [`color-scheme`](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme) style to inputs so they get the correct user-agent dark/light styles
6+
- crops ToggleSwitch knob's shadow inside the toggle switch boundaries
7+
- changes FormControl styles to prevent `<select>`, `<textarea>`, `<input>` from filling the parent's width when the `block` prop has not been passed to the input component

src/BaseStyles.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import React from 'react'
22
import styled, {createGlobalStyle} from 'styled-components'
33
import {COMMON, SystemCommonProps, SystemTypographyProps, TYPOGRAPHY} from './constants'
4+
import {useTheme} from './ThemeProvider'
45
import {ComponentProps} from './utils/types'
56

6-
const GlobalStyle = createGlobalStyle`
7+
const GlobalStyle = createGlobalStyle<{colorScheme?: 'light' | 'dark'}>`
78
* { box-sizing: border-box; }
89
body { margin: 0; }
910
table { border-collapse: collapse; }
10-
11+
input { color-scheme: ${props => props.colorScheme}; }
12+
1113
[role="button"]:focus:not(:focus-visible):not(.focus-visible),
1214
[role="tabpanel"][tabindex="0"]:focus:not(:focus-visible):not(.focus-visible),
1315
button:focus:not(:focus-visible):not(.focus-visible),
@@ -32,13 +34,14 @@ export type BaseStylesProps = ComponentProps<typeof Base>
3234

3335
function BaseStyles(props: BaseStylesProps) {
3436
const {children, ...rest} = props
37+
const {colorScheme} = useTheme()
3538

3639
// load polyfill for :focus-visible
3740
require('focus-visible')
3841

3942
return (
4043
<Base {...rest} data-portal-root>
41-
<GlobalStyle />
44+
<GlobalStyle colorScheme={colorScheme?.includes('dark') ? 'dark' : 'light'} />
4245
{children}
4346
</Base>
4447
)

src/FormControl/FormControl.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>(
174174
ref={ref}
175175
display="flex"
176176
flexDirection="column"
177+
alignItems="flex-start"
177178
sx={{...(isLabelHidden ? {'> *:not(label) + *': {marginTop: 1}} : {'> * + *': {marginTop: 1}}), ...sx}}
178179
>
179180
{slots.Label}

src/Select.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const ArrowIndicator = styled(ArrowIndicatorSVG)`
5656
`
5757

5858
const Select = React.forwardRef<HTMLSelectElement, SelectProps>(
59-
({children, disabled, placeholder, size, required, validationStatus, ...rest}: SelectProps, ref) => (
59+
({block, children, contrast, disabled, placeholder, size, required, validationStatus, ...rest}: SelectProps, ref) => (
6060
<TextInputWrapper
6161
sx={{
6262
overflow: 'hidden',
@@ -67,9 +67,11 @@ const Select = React.forwardRef<HTMLSelectElement, SelectProps>(
6767
}
6868
}
6969
}}
70+
block={block}
71+
contrast={contrast}
72+
disabled={disabled}
7073
size={size}
7174
validationStatus={validationStatus}
72-
disabled={disabled}
7375
>
7476
<StyledSelect
7577
ref={ref}

src/ToggleSwitch.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const SwitchButton = styled.button<SwitchButtonProps>`
9494
width: 64px;
9595
outline-offset: 2px;
9696
position: relative;
97+
overflow: hidden;
9798
9899
@media (pointer: coarse) {
99100
&:before {
@@ -115,17 +116,6 @@ const SwitchButton = styled.button<SwitchButtonProps>`
115116
}
116117
}
117118
118-
&:after {
119-
content: '';
120-
box-sizing: border-box;
121-
position: absolute;
122-
top: 0;
123-
left: 0;
124-
width: 100%;
125-
height: 100%;
126-
border-radius: calc(${get('radii.2')} - 1px); /* -1px to account for 1px border around the control */
127-
}
128-
129119
${props => {
130120
if (props.disabled) {
131121
return css`

src/_TextInputWrapper.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export const TextInputBaseWrapper = styled.span<StyledBaseWrapperProps>`
163163
css`
164164
width: 100%;
165165
display: flex;
166+
align-self: stretch;
166167
`}
167168
168169
// Ensures inputs don' t zoom on mobile but are body-font size on desktop

src/__tests__/__snapshots__/TextInput.test.tsx.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ exports[`TextInput renders block 1`] = `
144144
display: -webkit-flex;
145145
display: -ms-flexbox;
146146
display: flex;
147+
-webkit-align-self: stretch;
148+
-ms-flex-item-align: stretch;
149+
align-self: stretch;
147150
background-repeat: no-repeat;
148151
background-position: right 8px center;
149152
padding-left: 0;

src/__tests__/__snapshots__/TextInputWithTokens.test.tsx.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,9 @@ exports[`TextInputWithTokens renders as block layout 1`] = `
12081208
display: -webkit-flex;
12091209
display: -ms-flexbox;
12101210
display: flex;
1211+
-webkit-align-self: stretch;
1212+
-ms-flex-item-align: stretch;
1213+
align-self: stretch;
12111214
padding-left: 12px;
12121215
padding-top: calc(12px / 2);
12131216
padding-bottom: calc(12px / 2);

src/__tests__/__snapshots__/ToggleSwitch.test.tsx.snap

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,11 @@ exports[`renders consistently 1`] = `
133133
width: 64px;
134134
outline-offset: 2px;
135135
position: relative;
136+
overflow: hidden;
136137
background-color: #eaeef2;
137138
border-color: #afb8c1;
138139
}
139140
140-
.c4:after {
141-
content: '';
142-
box-sizing: border-box;
143-
position: absolute;
144-
top: 0;
145-
left: 0;
146-
width: 100%;
147-
height: 100%;
148-
border-radius: calc(6px - 1px);
149-
}
150-
151141
.c4:hover .Toggle-knob,
152142
.c4:focus:focus-visible .Toggle-knob {
153143
background-color: #f3f4f6;

0 commit comments

Comments
 (0)