Skip to content

Commit 97894d2

Browse files
authored
button toggle states (#1593)
Update hover state for button toggles and groups to distinguish from toggle states
1 parent 99f810a commit 97894d2

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

packages/components/src/Button/ButtonItem.tsx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
*/
2626

27-
import React, { forwardRef, MouseEvent, Ref, useContext } from 'react'
27+
import React, { forwardRef, MouseEvent, Ref, useContext, useState } from 'react'
2828
import styled from 'styled-components'
2929
import {
3030
CompatibleHTMLProps,
@@ -39,17 +39,30 @@ export interface ButtonItemProps
3939
extends SpaceProps,
4040
Omit<CompatibleHTMLProps<HTMLButtonElement>, 'type' | 'aria-pressed'> {
4141
value?: string
42+
focusVisible?: boolean
4243
}
4344

4445
const ButtonLayout = forwardRef(
4546
(
46-
{ children, onClick, value, ...props }: ButtonItemProps,
47+
{ children, onClick, value, onBlur, onKeyUp, ...props }: ButtonItemProps,
4748
ref: Ref<HTMLButtonElement>
4849
) => {
4950
const { disabled, value: contextValue, onItemClick } = useContext(
5051
ButtonSetContext
5152
)
5253

54+
const [isFocusVisible, setFocusVisible] = useState(false)
55+
56+
const handleOnKeyUp = (event: React.KeyboardEvent<HTMLButtonElement>) => {
57+
setFocusVisible(true)
58+
onKeyUp && onKeyUp(event)
59+
}
60+
61+
const handleOnBlur = (event: React.FocusEvent<HTMLButtonElement>) => {
62+
setFocusVisible(false)
63+
onBlur && onBlur(event)
64+
}
65+
5366
function handleClick(e: MouseEvent<HTMLButtonElement>) {
5467
onClick && onClick(e)
5568
if (!e.defaultPrevented) {
@@ -67,23 +80,31 @@ const ButtonLayout = forwardRef(
6780
: false
6881

6982
return (
70-
<button
71-
type="button"
83+
<ButtonOuter
7284
aria-pressed={selected}
7385
ref={ref}
7486
onClick={handleClick}
7587
value={itemValue}
7688
disabled={disabled}
89+
focusVisible={isFocusVisible}
90+
onKeyUp={handleOnKeyUp}
91+
onBlur={handleOnBlur}
7792
{...omitStyledProps(props)}
7893
>
7994
{children}
80-
</button>
95+
</ButtonOuter>
8196
)
8297
}
8398
)
8499

85100
ButtonLayout.displayName = 'ButtonLayout'
86101

102+
const ButtonOuter = styled.button<ButtonItemProps>`
103+
${(props) =>
104+
props.focusVisible &&
105+
`box-shadow: 0 0 0.5px 1px ${props.theme.colors.keyFocus}`}
106+
`
107+
87108
export const ButtonItem = styled(ButtonLayout)`
88109
align-items: center;
89110
background: transparent;
@@ -105,11 +126,11 @@ export const ButtonItem = styled(ButtonLayout)`
105126
&:active,
106127
&:focus,
107128
&:hover {
108-
background: ${({ theme }) => theme.colors.keyAccent};
129+
background: ${({ theme }) => theme.colors.neutralSubtle};
130+
color: ${({ theme }) => theme.colors.text5};
109131
}
110132
111133
&:focus {
112-
box-shadow: 0 0 0.5px 1px ${({ theme }) => theme.colors.keyFocus};
113134
outline: none;
114135
}
115136

packages/components/src/Button/__snapshots__/ButtonGroup.test.tsx.snap

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ exports[`ButtonGroup controlled 1`] = `
3434
.c3:active,
3535
.c3:focus,
3636
.c3:hover {
37-
background: #E8E5FF;
37+
background: #FBFBFC;
38+
color: #262D33;
3839
}
3940
4041
.c3:focus {
41-
box-shadow: 0 0 0.5px 1px #9785F2;
4242
outline: none;
4343
}
4444
@@ -107,23 +107,20 @@ exports[`ButtonGroup controlled 1`] = `
107107
<button
108108
aria-pressed="false"
109109
class="c2 c3"
110-
type="button"
111110
value="Apples"
112111
>
113112
Apples
114113
</button>
115114
<button
116115
aria-pressed="false"
117116
class="c2 c3"
118-
type="button"
119117
value="Oranges"
120118
>
121119
Oranges
122120
</button>
123121
<button
124122
aria-pressed="true"
125123
class="c2 c3"
126-
type="button"
127124
value="Bananas"
128125
>
129126
Bananas

packages/components/src/Button/__snapshots__/ButtonToggle.test.tsx.snap

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ exports[`ButtonToggle controlled 1`] = `
3434
.c3:active,
3535
.c3:focus,
3636
.c3:hover {
37-
background: #E8E5FF;
37+
background: #FBFBFC;
38+
color: #262D33;
3839
}
3940
4041
.c3:focus {
41-
box-shadow: 0 0 0.5px 1px #9785F2;
4242
outline: none;
4343
}
4444
@@ -146,23 +146,20 @@ exports[`ButtonToggle controlled 1`] = `
146146
<button
147147
aria-pressed="false"
148148
class="c2 c3"
149-
type="button"
150149
value="Apples"
151150
>
152151
Apples
153152
</button>
154153
<button
155154
aria-pressed="false"
156155
class="c2 c3"
157-
type="button"
158156
value="Oranges"
159157
>
160158
Oranges
161159
</button>
162160
<button
163161
aria-pressed="true"
164162
class="c2 c3"
165-
type="button"
166163
value="Bananas"
167164
>
168165
Bananas

0 commit comments

Comments
 (0)