Skip to content

Commit

Permalink
#4 - feat: add button components
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvandescheur committed Jan 11, 2024
1 parent 8a848f2 commit fdd8ed9
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 4 deletions.
56 changes: 56 additions & 0 deletions src/components/button/button.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@use '../../settings/style';

.mykn-button {
--mykn-button-color-background: var(--theme-color-primary-800);
--mykn-button-color-shadow: var(--theme-shade-1000);
--mykn-button-color-text: var(--theme-shade-0);
--mykn-button-offset: 0px;

appearance: none;
margin: 0;
padding: 12px;
background-color: var(--mykn-button-color-background);
border: 1px solid var(--mykn-button-color-border);
border-radius: 4px;
box-shadow: 0 calc(var(--mykn-button-offset) * -1) var(--mykn-button-color-shadow);
box-sizing: border-box;
color: var(--mykn-button-color-text);
cursor: pointer;
display: inline-block;
font-family: Inter, sans-serif;
font-size: var(--typography-font-size-body-s);
line-height: var(--typography-line-height-body-s);
text-decoration: none;
transition: all var(--animation-duration) var(--animation-timing-function);
transform: translateY(var(--mykn-button-offset));


&--variant-primary {
&:focus,
&:hover {
--mykn-button-color-background: var(--theme-color-primary-700);
--mykn-button-offset: -2px;
}

&:active {
--mykn-button-color-background: var(--theme-color-primary-800);
--mykn-button-offset: 0px;
}
}

&--variant-transparent {
--mykn-button-color-background: transparent;
--mykn-button-color-shadow: currentColor;
--mykn-button-color-text: var(--theme-shade-700);

&:focus,
&:hover {
--mykn-button-color-background: var(--theme-color-primary-200);
--mykn-button-offset: -2px;
}

&:active {
--mykn-button-offset: 0px;
}
}
}
66 changes: 66 additions & 0 deletions src/components/button/button.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { Meta, StoryObj } from "@storybook/react";
import { userEvent } from "@storybook/test";
import React from "react";

import { Button, ButtonLink } from "./button";

const meta = {
title: "Controls/Button",
component: Button,
} satisfies Meta<typeof Button>;

export default meta;
type Story = StoryObj<typeof meta>;

export const ButtonComponent: Story = {
args: {
children: "The quick brown fox jumps over the lazy dog.",
},
};

export const TransparentButton: Story = {
args: {
children: "The quick brown fox jumps over the lazy dog.",
variant: "transparent",
},
};

export const ButtonAnimatesOnHoverAndClick: Story = {
args: {
children: "The quick brown fox jumps over the lazy dog.",
},
play: async () => {
await userEvent.tab({ delay: 10 });
},
};

export const ButtonLinkComponent: StoryObj<typeof ButtonLink> = {
args: {
children: "The quick brown fox jumps over the lazy dog.",
href: "https://www.example.com",
target: "_blank",
},
render: (args) => <ButtonLink {...args} />,
};

export const TransparentButtonLink: StoryObj<typeof ButtonLink> = {
args: {
children: "The quick brown fox jumps over the lazy dog.",
href: "https://www.example.com",
target: "_blank",
variant: "transparent",
},
render: (args) => <ButtonLink {...args} />,
};

export const ButtonLinkAnimatesOnHoverAndClick: StoryObj<typeof ButtonLink> = {
args: {
children: "The quick brown fox jumps over the lazy dog.",
href: "https://www.example.com",
target: "_blank",
},
play: async () => {
await userEvent.tab({ delay: 10 });
},
render: (args) => <ButtonLink {...args} />,
};
73 changes: 73 additions & 0 deletions src/components/button/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import clsx from "clsx";
import React from "react";

import "./button.scss";

export type ButtonProps = React.PropsWithChildren<{
variant?: "primary" | "transparent";
}>;

/**
* Higher-order component (HOC) applying button logic to WrappedComponent
* @param WrappedComponent
* @private
*/
const withButtonBehavior = <P extends ButtonProps>(
WrappedComponent: React.ComponentType<P>,
) => {
// Define the enhanced component
const EnhancedButton: React.FC<P> = ({
children,
variant = "primary",
...props
}) => {
return (
<WrappedComponent
className={clsx("mykn-button", `mykn-button--variant-${variant}`)}
{...(props as P)}
>
{children}
</WrappedComponent>
);
};

return EnhancedButton;
};

/**
* Base template for Button component
* @private
*/
const BaseButton: React.FC<
ButtonProps & React.ButtonHTMLAttributes<HTMLButtonElement>
> = (props) => {
return <button {...props}>{props.children}</button>;
};

/**
* Base template for ButtonLink component
* @private
*/
const BaseButtonLink: React.FC<
ButtonProps & React.AnchorHTMLAttributes<HTMLAnchorElement>
> = (props) => {
return <a {...props}>{props.children}</a>;
};

/**
* Button component
* @param children
* @param type
* @param props
* @constructor
*/
export const Button = withButtonBehavior(BaseButton);

/**
* Anchor (<a>) version of button component
* @param children
* @param type
* @param props
* @constructor
*/
export const ButtonLink = withButtonBehavior(BaseButtonLink);
1 change: 1 addition & 0 deletions src/components/button/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./button";
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Auto-generated file. Do not modify manually.
export * from "./button";
export * from "./layout";
export * from "./logo";
export * from "./page";
Expand Down
4 changes: 1 addition & 3 deletions src/components/typography/a/a.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import React, { AnchorHTMLAttributes } from "react";

import "./a.scss";

export type AProps = React.PropsWithChildren<
AnchorHTMLAttributes<HTMLAnchorElement>
>;
export type AProps = AnchorHTMLAttributes<HTMLAnchorElement>;

/**
* Anchor (<a>) component
Expand Down
2 changes: 1 addition & 1 deletion src/settings/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:root {
/// ANIMATION
--animation-timing-function: ease-in-out;
--animation-duration: 0.2s;
--animation-duration: 0.1s;

/// THEME
--theme-color-primary-800: #341A90;
Expand Down

0 comments on commit fdd8ed9

Please sign in to comment.