Skip to content

Commit d8807c9

Browse files
committed
Use data-density
1 parent 1c17b5a commit d8807c9

3 files changed

Lines changed: 45 additions & 45 deletions

File tree

packages/eds-core-react/src/components/Checkbox/Checkbox.new.stories.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect, useRef, ChangeEvent } from 'react'
2-
import { Typography, Button, EdsProvider, Table, Density } from '../..'
2+
import { Typography, Button, Table } from '../..'
33
import { Checkbox } from './Checkbox.new'
44
import type { CheckboxProps } from './Checkbox.new.types'
55
import { action } from 'storybook/actions'
@@ -257,21 +257,22 @@ export const WithFormsControl: StoryFn<CheckboxProps> = () => {
257257
WithFormsControl.storyName = 'Example with React Hook Form'
258258

259259
export const Compact: StoryFn<CheckboxProps> = () => {
260-
const [density, setDensity] = useState<Density>('comfortable')
261-
262-
useEffect(() => {
263-
// Simulate user change
264-
setDensity('comfortable')
265-
}, [density])
266-
267260
return (
268-
<EdsProvider density={density}>
261+
<div data-density="comfortable">
269262
<Checkbox label="I am compact" />
270263
<Checkbox label="I am also compact" defaultChecked />
271264
<Checkbox label="I am compact and disabled" disabled />
272-
</EdsProvider>
265+
</div>
273266
)
274267
}
268+
Compact.parameters = {
269+
docs: {
270+
description: {
271+
story:
272+
'Compact mode is activated by adding `data-density="comfortable"` to a parent element. The density attribute is inherited by all child components.',
273+
},
274+
},
275+
}
275276

276277
export const AlternativeToLabel: StoryFn<CheckboxProps> = () => (
277278
<Checkbox aria-label="This label is invisible, but read by screen-readers" />

packages/eds-core-react/src/components/Checkbox/Checkbox.new.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
checkbox_indeterminate,
77
} from '@equinor/eds-icons'
88
import { TypographyNext } from '../Typography'
9-
import { useEds } from '../EdsProvider'
109
import type { CheckboxProps } from './Checkbox.new.types'
1110
import './checkbox.new.css'
1211

@@ -26,7 +25,6 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
2625
},
2726
ref,
2827
) {
29-
const { density } = useEds()
3028
const internalRef = useRef<HTMLInputElement>(null)
3129
const inputRef = (ref as React.RefObject<HTMLInputElement>) || internalRef
3230

@@ -116,12 +114,7 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
116114

117115
if (label) {
118116
return (
119-
<label
120-
className={wrapperClasses}
121-
style={style}
122-
data-density={density}
123-
{...labelProps}
124-
>
117+
<label className={wrapperClasses} style={style} {...labelProps}>
125118
{checkboxInput}
126119
<TypographyNext
127120
as="span"
@@ -140,7 +133,7 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
140133
}
141134

142135
return (
143-
<span className={wrapperClasses} style={style} data-density={density}>
136+
<span className={wrapperClasses} style={style}>
144137
{checkboxInput}
145138
</span>
146139
)

packages/eds-core-react/src/components/Checkbox/checkbox.new.css

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
@import '@equinor/eds-tokens/css/variables';
22
@import '@equinor/eds-tokens/css/foundation';
33

4-
/* Custom properties for checkbox-specific sizes */
5-
:root {
6-
/* Hover circle size - smaller than 48px touch target for visual balance */
7-
--checkbox-hover-size: 40px;
8-
}
9-
10-
[data-density='compact'] {
11-
--checkbox-hover-size: 32px;
12-
}
13-
144
/* Checkbox wrapper/label */
155
.checkbox {
166
display: inline-flex;
@@ -20,6 +10,16 @@
2010
position: relative;
2111
}
2212

13+
/* Spacious density mode (default/comfortable in TypeScript) */
14+
.checkbox[data-density='spacious'] {
15+
--checkbox-hover-size: 40px;
16+
}
17+
18+
/* Compact density mode (compact in TypeScript) */
19+
.checkbox[data-density='comfortable'] {
20+
--checkbox-hover-size: 32px;
21+
}
22+
2323
.checkbox--disabled {
2424
cursor: not-allowed;
2525
}
@@ -30,9 +30,18 @@
3030
grid: [input] 1fr / [input] 1fr;
3131
position: relative;
3232
isolation: isolate;
33+
}
34+
35+
/* Spacious density padding */
36+
.checkbox[data-density='spacious'] .checkbox__input-wrapper {
3337
padding: var(--eds-spacing-inset-xs-squared);
3438
}
3539

40+
/* Compact density padding */
41+
.checkbox[data-density='comfortable'] .checkbox__input-wrapper {
42+
padding: var(--eds-spacing-inset-2-xs-squared);
43+
}
44+
3645
/* Hover circle background */
3746
.checkbox__input-wrapper::before {
3847
content: '';
@@ -79,28 +88,25 @@
7988
outline: none;
8089
}
8190

82-
/* Focus styles */
83-
.checkbox__input[data-focus-visible-added]:focus ~ .checkbox__icon,
84-
.checkbox__input:focus-visible ~ .checkbox__icon {
91+
/* Focus styles - spacious (default) */
92+
.checkbox[data-density='spacious']
93+
.checkbox__input[data-focus-visible-added]:focus
94+
~ .checkbox__icon,
95+
.checkbox[data-density='spacious']
96+
.checkbox__input:focus-visible
97+
~ .checkbox__icon {
8598
outline: 2px dashed var(--eds-color-border-focus);
8699
outline-offset: var(--eds-spacing-inline-xs);
87100
}
88101

89-
/* Compact density mode */
90-
[data-density='compact'] .checkbox__input-wrapper {
91-
padding: var(--eds-spacing-inset-2-xs-squared);
92-
}
93-
94-
[data-density='compact'] .checkbox__input {
95-
/* Maintain 48px touch target in compact mode for accessibility */
96-
transform: scale(calc(48px / 24px));
97-
}
98-
99-
/* Density mode focus */
100-
[data-density='comfortable']
102+
/* Focus styles - compact */
103+
.checkbox[data-density='comfortable']
101104
.checkbox__input[data-focus-visible-added]:focus
102105
~ .checkbox__icon,
103-
[data-density='comfortable'] .checkbox__input:focus-visible ~ .checkbox__icon {
106+
.checkbox[data-density='comfortable']
107+
.checkbox__input:focus-visible
108+
~ .checkbox__icon {
109+
outline: 2px dashed var(--eds-color-border-focus);
104110
outline-offset: var(--eds-spacing-inline-3-xs);
105111
}
106112

0 commit comments

Comments
 (0)