Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to v2 components based on Sam's notes #1649

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/ui/src/Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { MouseEventHandler, useContext } from 'react';
import { MouseEventHandler } from 'react';
import styled, { css, DefaultTheme } from 'styled-components';
import { asTransientProps } from '../utils/asTransientProps';
import { Priority, ActionType, focusOutline, overlays, buttonBase } from '../utils/button';
import { getBackgroundColor } from './helpers';
import { button } from '../utils/typography';
import { LucideIcon } from 'lucide-react';
import { ButtonPriorityContext } from '../utils/ButtonPriorityContext';
import { Density } from '../types/Density';
import { useDensity } from '../hooks/useDensity';

Expand Down Expand Up @@ -108,6 +107,7 @@ interface BaseButtonProps {
actionType?: ActionType;
disabled?: boolean;
onClick?: MouseEventHandler<HTMLButtonElement>;
priority?: Priority;
}

interface IconOnlyProps {
Expand Down Expand Up @@ -167,8 +167,8 @@ export const Button = ({
iconOnly,
actionType = 'default',
type = 'button',
priority = 'primary',
}: ButtonProps) => {
const priority = useContext(ButtonPriorityContext);
const density = useDensity();

return (
Expand All @@ -179,7 +179,11 @@ export const Button = ({
onClick={onClick}
aria-label={iconOnly ? children : undefined}
title={iconOnly ? children : undefined}
$getFocusOutlineColor={theme => theme.color.action[outlineColorByActionType[actionType]]}
$getFocusOutlineColor={theme =>
iconOnly === 'adornment'
? theme.color.base.transparent
: theme.color.action[outlineColorByActionType[actionType]]
}
$getBorderRadius={theme =>
density === 'sparse' && iconOnly !== 'adornment'
? theme.borderRadius.sm
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/ButtonGroup/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const Basic: Story = {
actionType: 'default',
iconOnly: false,
column: false,
hasPrimaryButton: true,
buttons: [
{
label: 'Delegate',
Expand Down
49 changes: 32 additions & 17 deletions packages/ui/src/ButtonGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ActionType } from '../utils/button';
import { Button } from '../Button';
import styled from 'styled-components';
import { media } from '../utils/media';
import { ButtonPriorityContext } from '../utils/ButtonPriorityContext';
import { Density } from '../types/Density';
import { useDensity } from '../hooks/useDensity';

Expand Down Expand Up @@ -56,6 +55,14 @@ export interface ButtonGroupProps<IconOnly extends boolean> {
* narrow layout width, such as a dialog). In those cases, set this to `true`.
*/
column?: boolean;
/**
* Often, a button group should have a primary button for the action that the
* user is most likely to take. In those cases, pass `hasPrimaryButton` to
* `<ButtonGroup />`. When you do, the first button will have a `priority` of
* `primary`, while the rest will be `secondary`. (Note that there can only be
* one primary button in a button group.)
*/
hasPrimaryButton?: boolean;
}

const isIconOnly = (props: ButtonGroupProps<boolean>): props is ButtonGroupProps<true> =>
Expand All @@ -68,13 +75,14 @@ const isIconOnly = (props: ButtonGroupProps<boolean>): props is ButtonGroupProps
* When rendering multiple Penumbra UI buttons together, always use a
* `<ButtonGroup />` rather than individual `<Button />`s. This ensures that
* they always meet Penumbra UI guidelines. (For example, all buttons in a group
* should have the same `actionType`; and the first button in a group should be
* the `primary` priority, while subsequent buttons are the `secondary`
* should have the same `actionType`; and the first button in a group can have
* the `primary` priority, while subsequent buttons have the `secondary`
* priority.)
*/
export const ButtonGroup = ({
actionType = 'default',
column,
hasPrimaryButton,
...props
}: ButtonGroupProps<boolean>) => {
const density = useDensity();
Expand All @@ -88,24 +96,31 @@ export const ButtonGroup = ({
*/}
{isIconOnly(props) &&
props.buttons.map((button, index) => (
<ButtonPriorityContext.Provider key={index} value={index === 0 ? 'primary' : 'secondary'}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We got rid of the context, so now we just pass the priority directly to the button.

<ButtonWrapper $density={density} $iconOnly>
<Button icon={button.icon} actionType={actionType} onClick={button.onClick} iconOnly>
{button.label}
</Button>
</ButtonWrapper>
</ButtonPriorityContext.Provider>
<ButtonWrapper key={index} $density={density} $iconOnly>
<Button
icon={button.icon}
actionType={actionType}
onClick={button.onClick}
iconOnly
priority={index === 0 && hasPrimaryButton ? 'primary' : 'secondary'}
>
{button.label}
</Button>
</ButtonWrapper>
))}

{!isIconOnly(props) &&
props.buttons.map((button, index) => (
<ButtonPriorityContext.Provider key={index} value={index === 0 ? 'primary' : 'secondary'}>
<ButtonWrapper $density={density}>
<Button icon={button.icon} actionType={actionType} onClick={button.onClick}>
{button.label}
</Button>
</ButtonWrapper>
</ButtonPriorityContext.Provider>
<ButtonWrapper key={index} $density={density}>
<Button
icon={button.icon}
actionType={actionType}
onClick={button.onClick}
priority={index === 0 && hasPrimaryButton ? 'primary' : 'secondary'}
>
{button.label}
</Button>
</ButtonWrapper>
))}
</Root>
);
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/Dialog/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const Basic: Story = {
{ label: 'Secondary CTA', icon: Handshake },
{ label: 'Another secondary CTA', icon: Ban },
],
hasPrimaryButton: true,
}}
>
<WhiteTextWrapper>
Expand Down
14 changes: 9 additions & 5 deletions packages/ui/src/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Text } from '../Text';
import { Icon } from '../Icon';
import { X } from 'lucide-react';
import { ButtonGroup, ButtonGroupProps } from '../ButtonGroup';
import { Button } from '../Button';
import { Density } from '../Density';

const gradualBlur = (blur: string) => keyframes`
from {
Expand Down Expand Up @@ -226,11 +228,13 @@ const Content = <IconOnlyButtonGroupProps extends boolean | undefined>({
</RadixDialog.Title>

{showCloseButton && (
<RadixDialog.Close asChild>
<CloseButton aria-label='Close'>
<Icon IconComponent={X} size='md' />
</CloseButton>
</RadixDialog.Close>
<Density compact>
<RadixDialog.Close asChild>
<Button icon={X} iconOnly priority='secondary'>
Close
</Button>
</RadixDialog.Close>
</Density>
Comment on lines +223 to +229
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

)}
</TitleAndCloseButton>

Expand Down
14 changes: 0 additions & 14 deletions packages/ui/src/utils/ButtonPriorityContext.ts

This file was deleted.

Loading