Skip to content

Commit

Permalink
refactor(button): ♻️ update button related components (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy authored Jan 20, 2021
1 parent 60143f2 commit 7e39af2
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
stories: ["../src/**/stories/*.stories.@(ts|tsx)"],
stories: ["../src/Tag/stories/*.stories.@(ts|tsx)"],
addons: ["@storybook/addon-essentials"],
typescript: {
reactDocgen: false,
Expand Down
21 changes: 1 addition & 20 deletions preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ const renderlesskitConfig = {
"focus-within",
"lib:focus",
"focus",
"active",
],
},
plugins: [
Expand Down Expand Up @@ -490,30 +491,10 @@ const renderlesskitConfig = {
const variantName = variant ? `${variant}:${dataName}` : dataName;

addVariant(variantName, ({ modifySelectors, separator }) => {
// modifySelectors(({ className }) => {
// const selector = selectorClass => {
// // `.${e(`is-range-selection${separator}${className}`)}[data-is-range-selection]`
// if (dataBool)
// return `.${e(`${selectorClass}`)}[data-${dataName}]`;
// // `.${e(`aria-selected${separator}${className}`)}[aria-selected="true"]`
// return `.${e(`${selectorClass}`)}[${dataName}="true"]`;
// };
// // `aria-selected${separator}${className}`
// const dataClass = `${dataName}${separator}${className}`;
// // `lib${separator}aria-selected${separator}${className}`
// const variantDataClass = variant
// ? `${variant}${separator}${dataClass}`
// : dataClass;

// return selector(variantDataClass);
// });

const parser = selectorParser(selectors => {
selectors.walkClasses(sel => {
// `aria-selected${separator}${className}`
const dataClass = `${dataName}${separator}${sel.value}`;

// `lib${separator}aria-selected${separator}${className}`
if (variant) {
sel.value = `${variant}${separator}${dataClass}`;
} else {
Expand Down
26 changes: 2 additions & 24 deletions src/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import React from "react";
import { cx } from "@renderlesskit/react";
import { Button as AriaButton, ButtonProps as AriaButtonProps } from "reakit";

import { Box } from "../box";
import { useTheme } from "../theme";
import { Spinner } from "../spinner";
import { Box, BoxProps } from "../box";
import { useButtonGroup } from "./ButtonGroup";
import { forwardRefWithAs } from "../utils/types";
import { ButtonIcon } from "./ButtonIcon";

export type ButtonProps = Omit<AriaButtonProps, "prefix"> & {
/**
Expand Down Expand Up @@ -110,26 +111,3 @@ export const Button = forwardRefWithAs<

return <ButtonComp />;
});

export type ButtonIconProps = BoxProps & {};

export const ButtonIcon = forwardRefWithAs<
ButtonIconProps,
HTMLSpanElement,
"span"
>((props, ref) => {
const { children, ...rest } = props;

const _children = React.isValidElement(children)
? React.cloneElement(children, {
"aria-hidden": true,
focusable: false,
})
: children;

return (
<Box as="span" ref={ref} {...rest}>
{_children}
</Box>
);
});
15 changes: 6 additions & 9 deletions src/button/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { useTheme } from "../theme";
import { forwardRefWithAs } from "../utils/types";

export type ButtonGroupProps = BoxProps & {
/**
* Button will look collapsed together.
*/
isAttached?: boolean;
/**
* How large should the button be?
*/
Expand All @@ -19,12 +15,13 @@ export type ButtonGroupProps = BoxProps & {
* How the button should be styled?
*/
variant?: keyof Renderlesskit.GetThemeValue<"button", "variant">;
/**
* Button will look collapsed together.
*/
isAttached?: boolean;
};

export type ButtonGroupContext = Pick<
ButtonGroupProps,
"size" | "variant"
> & {};
export type ButtonGroupContext = Pick<ButtonGroupProps, "size" | "variant">;

const [ButtonGroupProvider, useButtonGroup] = createContext<ButtonGroupContext>(
{
Expand All @@ -43,7 +40,7 @@ export const ButtonGroup = forwardRefWithAs<ButtonGroupProps, "div">(
const theme = useTheme();
const buttonGroupStyles = cx(
theme.buttonGroup.base,
isAttached ? theme.buttonGroup.attached : "",
isAttached ? theme.buttonGroup.attached : "lib:space-x-1",
className,
);

Expand Down
28 changes: 28 additions & 0 deletions src/button/ButtonIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from "react";
import { Box, BoxProps } from "reakit";

import { forwardRefWithAs } from "../utils/types";

export type ButtonIconProps = BoxProps;

export const ButtonIcon = forwardRefWithAs<
ButtonIconProps,
HTMLSpanElement,
"span"
>((props, ref) => {
const { children, ...rest } = props;

const _children = React.isValidElement(children)
? React.cloneElement(children, {
"aria-hidden": true,
focusable: false,
role: "img",
})
: children;

return (
<Box as="span" ref={ref} {...rest}>
{_children}
</Box>
);
});
19 changes: 19 additions & 0 deletions src/button/CloseButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CloseIcon } from "../icons";
import { forwardRefWithAs } from "../utils/types";
import { IconButton, IconButtonProps } from "./IconButton";

export type CloseButtonProps = IconButtonProps;

export const CloseButton = forwardRefWithAs<
CloseButtonProps,
HTMLButtonElement,
"button"
>((props, ref) => {
const { children, ...rest } = props;

return (
<IconButton aria-label="Close" ref={ref} {...rest}>
{children || <CloseIcon />}
</IconButton>
);
});
18 changes: 1 addition & 17 deletions src/button/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from "react";
import { cx } from "@renderlesskit/react";
import { Button, ButtonProps } from "reakit";

import { CloseIcon } from "../icons";
import { useTheme } from "../theme";
import { useButtonGroup } from "./ButtonGroup";
import { forwardRefWithAs } from "../utils/types";
Expand Down Expand Up @@ -40,6 +39,7 @@ export const IconButton = forwardRefWithAs<
? React.cloneElement(children, {
"aria-hidden": true,
focusable: false,
role: "img",
})
: children;

Expand All @@ -49,19 +49,3 @@ export const IconButton = forwardRefWithAs<
</Button>
);
});

export type CloseButtonProps = IconButtonProps & {};

export const CloseButton = forwardRefWithAs<
CloseButtonProps,
HTMLButtonElement,
"button"
>((props, ref) => {
const { children, ...rest } = props;

return (
<IconButton aria-label="close" ref={ref} {...rest}>
{children || <CloseIcon />}
</IconButton>
);
});
4 changes: 3 additions & 1 deletion src/button/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./Button";
export * from "./IconButton";
export * from "./ButtonGroup";
export * from "./ButtonIcon";
export * from "./CloseButton";
export * from "./IconButton";
36 changes: 20 additions & 16 deletions src/button/stories/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,6 @@ const iconButtonBase = storyTemplate<IconButtonProps>(
);

export const OnlyIcon = iconButtonBase({});
const IconButtonGroupBase = storyTemplate<ButtonGroupProps>(args => (
<ButtonGroup {...args}>
<IconButton aria-label="search" {...args}>
<SearchIcon />
</IconButton>
<IconButton aria-label="info" {...args}>
<InfoCircleIcon />
</IconButton>
<IconButton aria-label="settings" {...args}>
<WheelIcon />
</IconButton>
</ButtonGroup>
));

const closeButtonBase = storyTemplate<IconButtonProps>(CloseButtonDefault, {
size: "lg",
Expand All @@ -136,14 +123,31 @@ const buttonGroupBase = storyTemplate<ButtonGroupProps>(
{ size: "lg", variant: "primary" },
);

export const GroupDefault = buttonGroupBase({ className: "space-x-4" });
export const GroupDefault = buttonGroupBase({});

export const GroupCollapsed = buttonGroupBase({ isAttached: true });

const IconButtonGroupBase = storyTemplate<ButtonGroupProps>(
args => (
<ButtonGroup {...args}>
<IconButton aria-label="search">
<SearchIcon />
</IconButton>
<IconButton aria-label="info">
<InfoCircleIcon />
</IconButton>
<IconButton aria-label="settings">
<WheelIcon />
</IconButton>
</ButtonGroup>
),
{ size: "lg", variant: "primary" },
);

export const IconButtonGroupDefault = IconButtonGroupBase({});

export const IconButtonGroupCollapsed = IconButtonGroupBase({
isAttached: true,
size: "lg",
variant: "primary",
});

export const GroupSecondary = buttonGroupBase({
Expand Down
22 changes: 11 additions & 11 deletions src/theme/defaultTheme/button.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const button = {
span: "lib:inline-block lib:cursor-not-allowed",
span: "inline-block cursor-not-allowed",
base:
"lib:inline-flex lib:items-center lib:justify-center lib:outline-none lib:appearance-none lib:transition-all lib:whitespace-nowrap lib:align-middle lib:select-none lib:disabled:cursor-not-allowed lib:disabled:opacity-40",
variant: {
Expand All @@ -18,18 +18,18 @@ export const button = {
xl:
"lib:h-12 lib:w-auto lib:min-w-12 lib:px-4 lib:text-base lib:font-medium lib:rounded-lg lib:shadow-sm",
},
group: "lib:focus:z-1 lib:-mr-px lib:shadow-none",
prefix: {
xs: "lib:inherit lib:mr-1.5",
sm: "lib:inherit lib:mr-2",
lg: "lib:inherit lib:mr-2",
xl: "lib:inherit lib:mr-2",
xs: "inherit mr-1.5",
sm: "inherit mr-2",
lg: "inherit mr-2",
xl: "inherit mr-2",
},
suffix: {
xs: "lib:inherit lib:ml-1.5",
sm: "lib:inherit lib:ml-2",
lg: "lib:inherit lib:ml-2",
xl: "lib:inherit lib:ml-2",
xs: "inherit ml-1.5",
sm: "inherit ml-2",
lg: "inherit ml-2",
xl: "inherit ml-2",
},
spinner: "lib:w-em lib:h-em text-current",
group: "lib:focus:z-1 lib:-mr-px lib:shadow-none",
spinner: "w-em h-em text-current",
};
2 changes: 1 addition & 1 deletion src/theme/defaultTheme/buttonGroup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const buttonGroup = {
base: "lib:inline-block lib:shadow-sm lib:rounded-lg",
base: "lib:inline-block lib:shadow-sm lib:rounded-lg ",
attached: "lib:collapse-border",
};

1 comment on commit 7e39af2

@vercel
Copy link

@vercel vercel bot commented on 7e39af2 Jan 20, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.