Skip to content

Commit 848fc8b

Browse files
authored
fix: update action button to use grid area to position notification badge (#9303)
* Revert "Revert "fix: update action button to use grid area to position notification badge (#8409)" (#8552)" This reverts commit 0cdf701. * add chromatic story * fix lint
1 parent bb5ca47 commit 848fc8b

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

packages/@react-spectrum/s2/chromatic/ActionButton.stories.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {ActionButton, ActionButtonProps, Avatar, Text} from '../src';
13+
import {ActionButton, ActionButtonProps, Avatar, NotificationBadge, Text} from '../src';
14+
import BellIcon from '../s2wf-icons/S2_Icon_Bell_20_N.svg';
15+
import CommentIcon from '../s2wf-icons/S2_Icon_Comment_20_N.svg';
1416
import {Fonts, NotificationBadges, UnsafeClassName} from '../stories/ActionButton.stories';
1517
import {generatePowerset} from '@react-spectrum/story-utils';
1618
import type {Meta, StoryObj} from '@storybook/react';
@@ -100,3 +102,13 @@ export const AvatarOnly: ActionButtonStory = {
100102
};
101103

102104
export {Fonts, UnsafeClassName, NotificationBadges};
105+
106+
export const NotificationBadgesCustomWidth: ActionButtonStory = {
107+
render: (args) => (
108+
<div className={style({display: 'flex', flexDirection: 'column', gap: 8})}>
109+
<ActionButton aria-label="Messages has new activity" styles={style({width: 200})} {...args}><CommentIcon /><NotificationBadge /></ActionButton>
110+
<ActionButton styles={style({width: 200})} {...args}><BellIcon /><NotificationBadge value={10} /></ActionButton>
111+
<ActionButton styles={style({width: 200})} {...args}><CommentIcon /><Text>Messages</Text><NotificationBadge value={5} /></ActionButton>
112+
<ActionButton styles={style({width: 200})} {...args}><Text>Notifications</Text><NotificationBadge value={105} /></ActionButton>
113+
</div>)
114+
};

packages/@react-spectrum/s2/src/ActionButton.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const btnStyles = style<ButtonRenderProps & ActionButtonStyleProps & Togg
7373
...focusRing(),
7474
...staticColor(),
7575
...controlStyle,
76-
position: 'relative',
76+
display: 'grid',
7777
justifyContent: 'center',
7878
flexShrink: {
7979
default: 1,
@@ -86,9 +86,21 @@ export const btnStyles = style<ButtonRenderProps & ActionButtonStyleProps & Togg
8686
isJustified: 0
8787
},
8888
fontWeight: 'medium',
89+
width: 'fit',
8990
userSelect: 'none',
9091
transition: 'default',
9192
forcedColorAdjust: 'none',
93+
position: 'relative',
94+
gridTemplateAreas: {
95+
default: ['icon text'],
96+
[iconOnly]: ['icon'],
97+
[textOnly]: ['text']
98+
},
99+
gridTemplateColumns: {
100+
default: ['auto', 'auto'],
101+
[iconOnly]: ['auto'],
102+
[textOnly]: ['auto']
103+
},
92104
backgroundColor: {
93105
default: {
94106
...baseColor('gray-100'),
@@ -230,8 +242,7 @@ export const btnStyles = style<ButtonRenderProps & ActionButtonStyleProps & Togg
230242
'--badgePosition': {
231243
type: 'width',
232244
value: {
233-
default: 'calc(self(paddingStart) + var(--iconWidth))',
234-
[iconOnly]: 'calc(self(minWidth)/2 + var(--iconWidth)/2)',
245+
default: '--iconWidth',
235246
[textOnly]: 'full'
236247
}
237248
},
@@ -311,19 +322,18 @@ export const ActionButton = forwardRef(function ActionButton(props: ActionButton
311322
[SkeletonContext, null],
312323
[TextContext, {styles:
313324
style({
314-
order: 1,
325+
gridArea: 'text',
315326
truncate: true,
316327
visibility: {
317328
isProgressVisible: 'hidden'
318329
}
319330
})({isProgressVisible})
320331
}],
321332
[IconContext, {
322-
render: centerBaseline({slot: 'icon', styles: style({order: 0})}),
333+
render: centerBaseline({slot: 'icon', styles: style({gridArea: 'icon'})}),
323334
styles: style({
324335
size: fontRelative(20),
325336
marginStart: '--iconMargin',
326-
flexShrink: 0,
327337
visibility: {
328338
isProgressVisible: 'hidden'
329339
}
@@ -333,8 +343,7 @@ export const ActionButton = forwardRef(function ActionButton(props: ActionButton
333343
size: avatarSize[size],
334344
styles: style({
335345
marginStart: '--iconMargin',
336-
flexShrink: 0,
337-
order: 0
346+
gridArea: 'icon'
338347
})
339348
}],
340349
[ImageContext, {
@@ -351,9 +360,10 @@ export const ActionButton = forwardRef(function ActionButton(props: ActionButton
351360
styles: style({
352361
position: 'absolute',
353362
top: '--badgeTop',
354-
insetStart: '--badgePosition',
355363
marginTop: 'calc((self(height) * -1)/2)',
356-
marginStart: 'calc((self(height) * -1)/2)',
364+
marginStart: 'calc(var(--iconMargin) * 2 + (self(height) * -1)/4)',
365+
gridColumnStart: 1,
366+
insetStart: '--badgePosition',
357367
visibility: {
358368
isProgressVisible: 'hidden'
359369
}

0 commit comments

Comments
 (0)