Skip to content

Commit de8073c

Browse files
committed
refactor: Icon with appearance
Replace the size prop with appearance. "size" only set size whereas "appearance" also sets a value for `--nl-icon-inset-block-start` that center aligns icons with text inline. Add dummy SVG icons, circle, square, and diamond that are "edge-to-edge" icons as well as "inset" versions that are inset by 2 in their viewBoxes. Replace all stories that used "size" with their "appearance" counterparts.
1 parent 3338592 commit de8073c

File tree

5 files changed

+324
-54
lines changed

5 files changed

+324
-54
lines changed

packages/components-css/icon-css/src/_mixin.scss

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
@mixin nl-icon {
2-
// Default to 1lh × 1lh
3-
--_nl-icon-block-size: 1lh;
4-
--_nl-icon-inline-size: 1lh;
2+
// Default to 1em × 1em
3+
--_nl-icon-block-size: 1em;
4+
--_nl-icon-inline-size: 1em;
55

66
align-items: center;
7-
8-
// background-color: color-mix(in srgb, tomato 25%, transparent);
97
block-size: var(--nl-icon-block-size, var(--_nl-icon-block-size));
108
color: var(--nl-icon-color, currentcolor);
119
display: inline-flex;
@@ -24,22 +22,32 @@
2422
color: currentColor;
2523
}
2624

25+
// The icon takes on the size of capital letters of the current font-family
26+
@mixin nl-icon--cap {
27+
--nl-icon-block-size: 1cap;
28+
--nl-icon-inline-size: 1cap;
29+
--nl-icon-inset-block-start: calc(-0.5 * (1cap - 1ex));
30+
}
31+
2732
// The icon takes on the size of the current font-size
28-
@mixin nl-icon--size-em {
33+
@mixin nl-icon--em {
2934
--nl-icon-block-size: 1em;
3035
--nl-icon-inline-size: 1em;
36+
--nl-icon-inset-block-start: calc(-0.5 * (1cap - 1ex));
3137
}
3238

3339
// The icon takes on the size of the x-height of the current font-family
34-
@mixin nl-icon--size-ex {
40+
@mixin nl-icon--ex {
3541
--nl-icon-block-size: 1ex;
3642
--nl-icon-inline-size: 1ex;
43+
--nl-icon-inset-block-start: calc(-0.5 * (1cap - 1ex));
3744
}
3845

3946
// The icon takes on the size of the current line-height
40-
@mixin nl-icon--size-lh {
47+
@mixin nl-icon--lh {
4148
--nl-icon-block-size: 1lh;
4249
--nl-icon-inline-size: 1lh;
50+
--nl-icon-inset-block-start: calc(-0.5 * (1cap - 1ex));
4351
}
4452

4553
@mixin nl-icon--bidi-mirrored {

packages/components-css/icon-css/src/icon.scss

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010
}
1111
}
1212

13-
.nl-icon--size-em {
14-
@include mixin.nl-icon--size-em;
13+
.nl-icon--cap {
14+
@include mixin.nl-icon--cap;
1515
}
1616

17-
.nl-icon--size-ex {
18-
@include mixin.nl-icon--size-ex;
17+
.nl-icon--em {
18+
@include mixin.nl-icon--em;
1919
}
2020

21-
.nl-icon--size-lh {
22-
@include mixin.nl-icon--size-lh;
21+
.nl-icon--ex {
22+
@include mixin.nl-icon--ex;
23+
}
24+
25+
.nl-icon--lh {
26+
@include mixin.nl-icon--lh;
2327
}
2428

2529
.nl-icon--bidi-mirrored:dir(rtl) {

packages/components-react/icon-react/src/icon.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { clsx } from 'clsx';
22
import type { HTMLAttributes } from 'react';
33
import { forwardRef } from 'react';
44

5-
export type IconSize = 'em' | 'ex' | 'lh';
5+
export type IconAppearance = 'cap' | 'em' | 'ex' | 'lh';
66

77
export type IconProps = (
88
| (Omit<HTMLAttributes<HTMLSpanElement>, 'role' | 'aria-hidden' | 'aria-label' | 'aria-labelledby' | 'dir'> & {
@@ -24,20 +24,20 @@ export type IconProps = (
2424
role: 'img';
2525
})
2626
) & {
27+
appearance?: IconAppearance;
2728
/**
2829
* If `true` flips the icon horizontally in right-to-left layouts.
2930
* This is useful for directional icons like arrows or chevrons.
3031
*/
3132
bidiMirrored?: boolean;
32-
size?: IconSize;
3333
};
3434

3535
function hasRoleImg(props: IconProps): props is Extract<IconProps, { role: 'img' }> {
3636
return props.role === 'img';
3737
}
3838

3939
export const Icon = forwardRef<HTMLSpanElement, IconProps>(function Icon(props, forwardedRef) {
40-
const { bidiMirrored = false, children, className, size, ...restProps } = props;
40+
const { bidiMirrored = false, children, className, appearance, ...restProps } = props;
4141
const ariaHidden = hasRoleImg(props) ? undefined : (true as const);
4242

4343
return (
@@ -47,9 +47,10 @@ export const Icon = forwardRef<HTMLSpanElement, IconProps>(function Icon(props,
4747
'nl-icon',
4848
{
4949
['nl-icon--bidi-mirrored']: bidiMirrored,
50-
['nl-icon--size-em']: size === 'em',
51-
['nl-icon--size-ex']: size === 'ex',
52-
['nl-icon--size-lh']: size === 'lh',
50+
['nl-icon--cap']: appearance === 'cap',
51+
['nl-icon--em']: appearance === 'em',
52+
['nl-icon--ex']: appearance === 'ex',
53+
['nl-icon--lh']: appearance === 'lh',
5354
},
5455
className,
5556
)}

0 commit comments

Comments
 (0)