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

feat(styles): with variants #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions src/Button/Button.re
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
open Customize;

module Css = ButtonStyles;

[@react.component]
let make = (~primary=false, ~children) => {
let make =
(
~variant=Variants.Default,
~onClick=_ => ignore(),
~className="",
~children,
) => {
let theme = ThemeContext.useTheme();

<button
className={Cx.merge([|
Css.default(~theme),
primary ? Css.primary(~theme) : "",
|])}>
children
</button>;
let classes = Css.getClasses(variant, theme, className);

<button className=classes> children </button>;
};
57 changes: 50 additions & 7 deletions src/Button/ButtonStyles.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,64 @@ open Emotion;

let default = (~theme: ThemeTypes.theme) => [%css
[
color(`hex(theme.text.default)),
color(`hex(theme.colors.common.black)),
margin(`rem(0.5)),
padding2(`rem(0.5), `rem(1.5)),
borderRadius(`rem(2.)),
fontSize(`rem(1.)),
borderColor(`hex(theme.action.default)),
borderStyle(`solid),
borderStyle(`none),
cursor(`pointer),
backgroundColor(`hex(theme.background.default)),
backgroundColor(`hex(theme.action.default)),
fontWeight(600),
/* focus */
focus([outline(`rem(0.), `solid, `hex(theme.colors.primary.main))]),
/* hover */
hover([backgroundColor(`hex(Colors.grey.light))]),
]
];

let primary = (~theme: ThemeTypes.theme) => [%css
[
color(`hex(theme.text.primary)),
backgroundColor(`hex(theme.action.primary)),
color(`hex(theme.colors.primary.contrastText)),
borderStyle(`none),
backgroundColor(`hex(theme.colors.primary.main)),
/* hover */
hover([backgroundColor(`hex(theme.colors.primary.light))]),
]
];
];

let secondary = (~theme: ThemeTypes.theme) => [%css
[
color(`hex(theme.colors.secondary.contrastText)),
borderStyle(`none),
backgroundColor(`hex(theme.colors.secondary.main)),
/* hover */
hover([backgroundColor(`hex(theme.colors.secondary.light))]),
]
];

let disabled = (~theme: ThemeTypes.theme) => [%css
[
color(`hex(theme.colors.grey.main)),
borderStyle(`none),
backgroundColor(`hex(theme.colors.grey.light)),
cursor(`notAllowed),
/* hover */
hover([backgroundColor(`hex(theme.colors.grey.light))]),
]
];

let getClasses = (variant, theme, className) => {
let defaultClass = default(theme);

let classByVariant =
Variants.(
switch (variant) {
| Primary => primary(theme)
| Secondary => secondary(theme)
| Disabled => disabled(theme)
| _ => ""
}
);
Customize.mergeStyles([|defaultClass, classByVariant, className|]);
Copy link
Member

Choose a reason for hiding this comment

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

we could keep using cx.merge directly then

};
2 changes: 1 addition & 1 deletion src/Theme/Theme.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let light: ThemeTypes.theme = {
action: {
primary: "000088",
secondary: "ffff88",
default: Colors.common.white,
default: Colors.grey.main,
disabled: "575757",
},
text: {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/Utils/Customize.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let mergeStyles = Cx.merge;
Copy link
Member

Choose a reason for hiding this comment

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

see upper comment, it becomes useless this way, I did that to standardise the approach about custom user given className and the theme generated variant one

7 changes: 7 additions & 0 deletions src/Utils/Variants.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type classes = list(string);

type variants =
| Primary
| Secondary
| Disabled
| Default;
17 changes: 16 additions & 1 deletion stories/ButtonShowcase.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
let default = {"title": "Button", "excludeStories": [|"$$default"|]};

let normal = () =>
<ThemeProvider> <Button> {React.string("Hello")} </Button> </ThemeProvider>;

let primary = () =>
<ThemeProvider> <Button> {React.string("Hello")} </Button> </ThemeProvider>;
<ThemeProvider>
<Button variant=Variants.Primary> {React.string("Hello")} </Button>
</ThemeProvider>;

let secondary = () =>
<ThemeProvider>
<Button variant=Variants.Secondary> {React.string("Hello")} </Button>
</ThemeProvider>;

let disabled = () =>
<ThemeProvider>
<Button variant=Variants.Disabled> {React.string("Hello")} </Button>
</ThemeProvider>;