Skip to content

Instantly share code, notes, and snippets.

@kaw2k
Last active March 23, 2017 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaw2k/1a42248f93c75cd6f434bfde58d5cff0 to your computer and use it in GitHub Desktop.
Save kaw2k/1a42248f93c75cd6f434bfde58d5cff0 to your computer and use it in GitHub Desktop.
import * as React from 'react';
import * as cx from 'classnames';
import Icon, { IconOptions } from './icon';
export interface IClickableIconProps {
iconName?: IconOptions;
iconPosition?: 'before' | 'after';
}
export const addClickableIcon = (children: React.ReactNode, iconName?: IconOptions, iconPosition: 'before' | 'after' = 'before') => {
if (!iconName) { return children; };
const arr = React.Children.toArray(children);
const icon = <Icon iconName={iconName} className={`icon-${iconPosition}`} />;
return iconPosition === 'after' ? [...arr, icon] : [icon, ...arr];
};
export type IClickableStyleProps = {
theme?: 'blue' | 'light-blue' | 'green' | 'yellow' | 'orange' | 'white';
flat?: boolean; // Inverses the button and removes backgrounds
size?: 'inline' | 'auto' | 'base';
flow?: 'horizontal' | 'vertical';
}
export const clickableStyles = ({ flat = false, size, flow, theme }: IClickableStyleProps, ...classes: string[]) =>
cx('clickable', ...classes, {
'clickable--flat': flat,
[`clickable--size-${size}`]: !!size,
[`clickable--theme-${theme}`]: !!theme,
[`clickable--flow-${flow}`]: !!flow,
});
import * as React from 'react';
import { IClickableIconProps, IClickableStyleProps, addClickableIcon, clickableStyles } from './clickable';
import IHTML from '../interfaces/html';
export interface IButtonProps extends IClickableIconProps, IClickableStyleProps, IHTML {
type?: 'submit' | 'button';
disabled?: boolean;
tabIndex?: number;
onClick?: (e: React.MouseEvent<HTMLElement>) => void;
}
const Button: React.SFC<IButtonProps> = ({
children,
flat,
flow,
iconName,
iconPosition,
theme,
size = 'base',
className = '',
type = 'button',
...props
}) => {
const classes = clickableStyles({ flat, size, flow, theme }, 'button', className);
return <button {...props} type={type} className={classes}>
{addClickableIcon(children, iconName, iconPosition)}
</button>;
};
export default Button;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment