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

Top icon in MdButton #117

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 13 additions & 5 deletions packages/css/src/button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

To use the `Button` css in `@miljodirektoratet/md-css` as a standalone, without the accompanying React component, please use the following HTML structure.

Class names in brackets [] are optional-/togglable-/decorator- or state dependant classes.
Class names and elements in brackets [] are optional-/togglable-/decorator- or state dependant classes.

See [Storybook](https://miljodir.github.io/md-components) for examples and more info.

```html
<button className="md-button [md-button--small, md-button--secondary, md-button--danger]">
<div className="md-button__leftIcon">{leftIcon}</div>
BUTTON TEXT
<div className="md-button__rightIcon">{rightIcon}</div>
<button className="md-button [md-button--small, md-button--secondary, md-button--danger, md-button--column]">
[
<div className="md-button__topIcon">{topIcon}</div>
] [
<div className="md-button__content">
]
<div className="md-button__leftIcon">{leftIcon}</div>
BUTTON TEXT
<div className="md-button__rightIcon">{rightIcon}</div>
[
</div>
]
</button>
```
17 changes: 17 additions & 0 deletions packages/css/src/button/button.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@
border: none;
}

.md-button--column {
flex-direction: column;
gap: 0.5rem;
}

.md-button__content {
display: flex;
flex-direction: row;
align-items: center;
}

.md-button--small {
padding: 0.4rem 0.8rem;
border-radius: 0.43rem;
}

.md-button__topIcon {
height: 1rem;
width: 1rem;
flex-shrink: 0;
}

.md-button__leftIcon {
height: 1rem;
width: 1rem;
Expand Down
37 changes: 28 additions & 9 deletions packages/react/src/button/MdButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface MdButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElem
theme?: string;
leftIcon?: React.ReactNode;
rightIcon?: React.ReactNode;
topIcon?: React.ReactNode;
children?: string | React.ReactNode;
small?: boolean;
type?: 'button' | 'submit' | 'reset' | undefined;
Expand All @@ -14,6 +15,7 @@ const MdButton: React.FunctionComponent<MdButtonProps> = ({
theme,
leftIcon,
rightIcon,
topIcon,
children,
small,
type = 'button',
Expand All @@ -25,25 +27,42 @@ const MdButton: React.FunctionComponent<MdButtonProps> = ({
'md-button--small': !!small,
'md-button--secondary': theme === 'secondary',
'md-button--danger': theme === 'danger',
'md-button--column': !!topIcon,
},
otherProps.className,
);

return (
<button type={type} {...otherProps} className={classNames}>
{leftIcon && (
<div aria-hidden="true" className="md-button__leftIcon">
{leftIcon}
</div>
)}
{children}
{rightIcon && (
<div aria-hidden="true" className="md-button__rightIcon">
{rightIcon}
{topIcon && (
<div aria-hidden="true" className="md-button__topIcon">
{topIcon}
</div>
)}
<ConditionalWrapper
condition={!!topIcon}
wrap={wrappedChildren => {
return <div className="md-button__content">{wrappedChildren}</div>;
}}
>
{leftIcon && (
<div aria-hidden="true" className="md-button__leftIcon">
{leftIcon}
</div>
)}
{children}
{rightIcon && (
<div aria-hidden="true" className="md-button__rightIcon">
{rightIcon}
</div>
)}
</ConditionalWrapper>
</button>
);
};

export default MdButton;

const ConditionalWrapper = ({ condition, wrap, children }) => {
return condition ? wrap(children) : children;
};
31 changes: 31 additions & 0 deletions stories/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import Readme from '../packages/css/src/button/README.md';
import MdButton from '../packages/react/src/button/MdButton';
import MdChevronIcon from '../packages/react/src/icons/MdChevronIcon';
import MdXIcon from '../packages/react/src/icons/MdXIcon';

const markdownString =
// eslint-disable-next-line quotes
Expand Down Expand Up @@ -87,6 +88,14 @@ export default {
},
},
},
topIcon: {
description: 'Icon above label',
table: {
type: {
summary: 'DomElement | image | ReactNode',
},
},
},
},
};

Expand Down Expand Up @@ -118,6 +127,20 @@ const TemplateWithIcon = (args: ButtonArgs) => {
);
};

const TemplateWithTopIcon = (args: ButtonArgs) => {
return (
<MdButton
onClick={action(args.label)}
disabled={args.disabled}
theme={args.theme}
small={args.small}
topIcon={<MdXIcon />}
>
{args.label}
</MdButton>
);
};

export const Primary = Template.bind({});
Primary.args = {
theme: 'primary',
Expand Down Expand Up @@ -157,3 +180,11 @@ ButtonWithIcon.args = {
disabled: false,
small: false,
};

export const ButtonWithTopIcon = TemplateWithTopIcon.bind({});
ButtonWithTopIcon.args = {
theme: 'primary',
label: 'Knapp med ikon',
disabled: false,
small: false,
};
Loading
Loading