Skip to content

Commit

Permalink
fix logic for ButtonGroup and ButtonLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoskolodny committed Sep 11, 2024
1 parent 8449eb7 commit 8164a40
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 35 deletions.
6 changes: 1 addition & 5 deletions src/button-group.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ export const centerInDesktop = style({

export const container = style({
marginTop: `-${buttonLayoutSpacing}`,
marginLeft: `calc(-1 * (${buttonLayoutSpacing} + ${buttonLinkPadding}))`,
});

export const buttons = style({
marginLeft: buttonLinkPadding,
marginLeft: `calc(-1 * ${buttonLayoutSpacing})`,
});

export const buttonChild = style({
Expand Down
11 changes: 9 additions & 2 deletions src/button-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as React from 'react';
import classNames from 'classnames';
import {getPrefixedDataAttributes} from './utils/dom';
import * as styles from './button-group.css';
import * as buttonStyles from './button.css';

import type {ButtonLink, ButtonPrimary, ButtonSecondary} from './button';
import type {ByBreakpoint, DataAttributes, RendersNullableElement} from './utils/types';
Expand All @@ -24,6 +25,7 @@ const ButtonGroup = ({
}: ButtonGroupProps): JSX.Element | null => {
const anyAction = !!primaryButton || !!secondaryButton || !!link;
const bothButtons = !!primaryButton && !!secondaryButton;
const noButtons = !primaryButton && !secondaryButton;

const alignByBreakpoint =
typeof align === 'string'
Expand All @@ -48,13 +50,18 @@ const ButtonGroup = ({
{...getPrefixedDataAttributes(dataAttributes, 'ButtonGroup')}
>
{(primaryButton || secondaryButton) && (
<div className={classNames(styles.inline, styles.buttons)}>
<div className={classNames(styles.inline)}>
{primaryButton && <div className={styles.buttonChild}>{primaryButton}</div>}
{secondaryButton && <div className={styles.buttonChild}>{secondaryButton}</div>}
</div>
)}
{link && (
<div className={styles.buttonChild} style={{width: bothButtons ? '100%' : 'auto'}}>
<div
className={classNames(styles.buttonChild, {
[buttonStyles.forceLinkBleedLeft]: noButtons || bothButtons,
})}
style={{width: bothButtons ? '100%' : 'auto'}}
>
{link}
</div>
)}
Expand Down
35 changes: 8 additions & 27 deletions src/button-layout.css.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {style, globalStyle, styleVariants, createVar} from '@vanilla-extract/css';
import {style, globalStyle, styleVariants} from '@vanilla-extract/css';
import {sprinkles} from './sprinkles.css';
import * as mq from './media-queries.css';
import * as buttonStyles from './button.css';

const buttonLayoutSpacing = '16px';
const buttonLinkPadding = createVar();

export const vars = {buttonLinkPadding};

export const margins = style({
margin: '16px 0',
Expand Down Expand Up @@ -80,33 +78,16 @@ globalStyle(`${alignVariant['full-width']} > *:not(${linkBase})`, {
},
});

const bleedLeft = {
marginLeft: `calc(${buttonLayoutSpacing} / 2 - ${buttonLinkPadding})`,
};
const bleedRight = {
marginRight: `calc(${buttonLayoutSpacing} / 2 - ${buttonLinkPadding})`,
};

export const link = style([
linkBase,
{
width: 'auto',
},
]);

export const linkInNewLine = style([
linkBase,
{
selectors: {
[`${alignVariant.right} &`]: bleedRight,

[`${alignVariant.left} &`]: bleedLeft,
// in desktop, full-width is equivalent to left
[`${alignVariant['full-width']} &`]: {
'@media': {
[mq.desktopOrBigger]: bleedLeft,
},
},
},
},
]);
export const linkInNewLine = styleVariants({
center: [linkBase],
left: [linkBase, buttonStyles.forceLinkBleedLeft],
right: [linkBase, buttonStyles.forceLinkBleedRight],
'full-width': [linkBase, buttonStyles.forceLinkBleedLeftOnlyDesktop],
});
2 changes: 1 addition & 1 deletion src/button-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const ButtonLayout = ({

const linkContainer = link ? (
<div
className={classnames(numberOfButtons !== 1 ? styles.linkInNewLine : styles.link)}
className={classnames(numberOfButtons !== 1 ? styles.linkInNewLine[align] : styles.link)}
data-link="true"
>
{link}
Expand Down
37 changes: 37 additions & 0 deletions src/button.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,43 @@ globalStyle(`${textContent} svg`, {
display: 'block',
});

// These classes are used to force the bleed conditionally in ButtonGroup and ButtonLayout components
export const forceLinkBleedLeft = style({});
export const forceLinkBleedRight = style({});
export const forceLinkBleedLeftOnlyDesktop = style({});

globalStyle(`${forceLinkBleedLeft} button, ${forceLinkBleedLeft} a`, {
marginLeft: `calc(-1 * (${borderSize} + ${buttonPaddingX.default}))`,
});

globalStyle(`${forceLinkBleedLeft} button.${small}, ${forceLinkBleedLeft} a.${small}`, {
marginLeft: `calc(-1 * (${borderSize} + ${buttonPaddingX.small}))`,
});

globalStyle(`${forceLinkBleedRight} button, ${forceLinkBleedRight} a`, {
marginRight: `calc(-1 * (${borderSize} + ${buttonPaddingX.default}))`,
});

globalStyle(`${forceLinkBleedRight} button.${small}, ${forceLinkBleedRight} a.${small}`, {
marginRight: `calc(-1 * (${borderSize} + ${buttonPaddingX.small}))`,
});

globalStyle(`${forceLinkBleedLeftOnlyDesktop} button, ${forceLinkBleedLeftOnlyDesktop} a`, {
'@media': {
[mq.desktopOrBigger]: {
marginLeft: `calc(-1 * (${borderSize} + ${buttonPaddingX.default}))`,
},
},
});

globalStyle(`${forceLinkBleedLeftOnlyDesktop} button.${small}, ${forceLinkBleedLeftOnlyDesktop} a.${small}`, {
'@media': {
[mq.desktopOrBigger]: {
marginLeft: `calc(-1 * (${borderSize} + ${buttonPaddingX.small}))`,
},
},
});

const lightPrimary: ComplexStyleRule = [
button,
sprinkles({
Expand Down

0 comments on commit 8164a40

Please sign in to comment.