Skip to content

Instantly share code, notes, and snippets.

@xavierzwirtz
Created May 6, 2019 02:26
Show Gist options
  • Save xavierzwirtz/309fa88b3f91ff5a590f131152ed5ea1 to your computer and use it in GitHub Desktop.
Save xavierzwirtz/309fa88b3f91ff5a590f131152ed5ea1 to your computer and use it in GitHub Desktop.
// converted from https://github.com/mui-org/material-ui/tree/master/docs/src/pages/getting-started/page-layout-examples/dashboard
// view https://material-ui.com/getting-started/page-layout-examples/dashboard/
module L = Link;
open MaterialUi;
module Theme = MaterialUi_Theme.Theme;
module Mixins = MaterialUi_Theme.Mixins;
module ZIndex = MaterialUi_Theme.ZIndex;
module Spacing = MaterialUi_Theme.Spacing;
module Breakpoints = MaterialUi_Theme.Breakpoints;
module PaletteColor = MaterialUi_Theme.PaletteColor;
module Palette = MaterialUi_Theme.Palette;
module Transitions = MaterialUi_Theme.Transitions;
module Easing = MaterialUi_Theme.Easing;
module Duration = MaterialUi_Theme.Duration;
let drawerWidth = 240;
let px_of_float = x => x->int_of_float->string_of_int ++ "px";
let style = ReactDOMRe.Style.make;
[%mui.withStyles
"EnterpriseDashboardStyles"(theme =>
{
root: style(~display="flex", ()),
toolbar: style(~paddingRight="24px", ()), // keep right padding when drawer closed
toolbarIcon:
ReactDOMRe.Style.combine(
style(
~display="flex",
~alignItems="center",
~justifyContent="flex-end",
~padding="0 8px",
(),
),
theme->Theme.mixinsGet->Mixins.toolbarGet,
),
appBar:
style(
~zIndex=
(theme->Theme.zIndexGet->ZIndex.drawerGet +. 1.0)
->int_of_float
->string_of_int,
~transition=
Style.transitionCreate(
~theme,
~affectWidth=true,
~affectMargin=true,
~easing=
theme
->Theme.transitionsGet
->Transitions.easingGet
->Easing.sharpGet,
~duration=
theme
->Theme.transitionsGet
->Transitions.durationGet
->Duration.leavingScreenGet,
(),
),
(),
),
appBarShift:
style(
~marginLeft=drawerWidth->string_of_int ++ "px",
~width={j|calc(100% - $(drawerWidth)px)|j},
~transition=
Style.transitionCreate(
~theme,
~affectWidth=true,
~affectMargin=true,
~easing=
theme
->Theme.transitionsGet
->Transitions.easingGet
->Easing.sharpGet,
~duration=
theme
->Theme.transitionsGet
->Transitions.durationGet
->Duration.enteringScreenGet,
(),
),
(),
),
menuButton: style(~marginLeft="12px", ~marginRight="36px", ()),
menuButtonHidden: style(~display="none", ()),
title: style(~flexGrow="1", ()),
drawerPaper:
style(
~position="relative",
~whiteSpace="nowrap",
~width={j|$(drawerWidth)px|j},
~transition=
Style.transitionCreate(
~theme,
~affectWidth=true,
~easing=
theme
->Theme.transitionsGet
->Transitions.easingGet
->Easing.sharpGet,
~duration=
theme
->Theme.transitionsGet
->Transitions.durationGet
->Duration.enteringScreenGet,
(),
),
(),
),
drawerPaperClose:
style(
~overflowX="hidden",
~transition=
Style.transitionCreate(
~theme,
~affectWidth=true,
~affectMargin=true,
~easing=
theme
->Theme.transitionsGet
->Transitions.easingGet
->Easing.sharpGet,
~duration=
theme
->Theme.transitionsGet
->Transitions.durationGet
->Duration.leavingScreenGet,
(),
),
~width=
(theme->Theme.spacingGet->Spacing.unitGet *. 7.0)->px_of_float,
(),
)
->Style.addBreakpoint(
~theme,
~breakpoint=`SM,
~style=
style(
~width=
(theme->Theme.spacingGet->Spacing.unitGet *. 9.0)
->px_of_float,
(),
),
),
appBarSpacer: theme->Theme.mixinsGet->Mixins.toolbarGet,
content:
style(
~flexGrow="1",
~padding=
(theme->Theme.spacingGet->Spacing.unitGet *. 3.0)->px_of_float,
~height="100vh",
~overflow="auto",
(),
),
h5:
style(
~marginBottom=
(theme->Theme.spacingGet->Spacing.unitGet *. 2.0)->px_of_float,
(),
),
}
)
];
type state = {isOpen: bool};
type action =
| Open
| Close;
[@bs.deriving jsConverter]
type color = [
| [@bs.as "default"] `Default
| [@bs.as "error"] `Error
| [@bs.as "inherit"] `Inherit
| [@bs.as "primary"] `Primary
| [@bs.as "secondary"] `Secondary
| [@bs.as "textPrimary"] `TextPrimary
| [@bs.as "textSecondary"] `TextSecondary
];
[@bs.deriving jsConverter]
type fontSize = [
| [@bs.as "default"] `Default
| [@bs.as "inherit"] `Inherit
| [@bs.as "small"] `Small
| [@bs.as "large"] `Large
];
/* module Icon = {
[@bs.module "@material-ui/icons/AccessAlarm"][@react.component]
external make: (
~className: string=?,
~color: color=?,
~fontSize: fontSize=?,
~nativeColor: string=?,
~style: ReactDOMRe.Style.t=?,
~titleAccess: string=?,
~viewBox: string=?,
) => React.element = "default";
} */
[@react.component]
let make = (~sidebar, ~children) => {
let (state, setState) = React.useState(() => {isOpen: true});
let setState = action => {
setState(state =>
switch (action) {
| Open => {isOpen: true}
| Close => {isOpen: false}
},
);
};
<EnterpriseDashboardStyles>
...{classes =>
<div className={classes.root}>
<CssBaseline />
<AppBar
position=`Absolute
className={Cn.make([
classes.appBar,
classes.appBarShift->Cn.ifTrue(state.isOpen),
])}>
<Toolbar
disableGutters={!state.isOpen} className={classes.toolbar}>
<IconButton
color=`Inherit
onClick={_event => setState(Open)}
className={Cn.make([
classes.menuButton,
classes.menuButtonHidden->Cn.ifTrue(state.isOpen),
])}>
<MscharleyBsMaterialUiIcons.Menu.Filled />
</IconButton>
<MaterialUi_Typography
component={`String("h1")}
variant=`H6
color=`Inherit
noWrap=true
className={classes.title}>
"Dashboard"->ReasonReact.string
</MaterialUi_Typography>
<IconButton color=`Inherit>
<Badge badgeContent={"4"->ReasonReact.string} color=`Secondary>
<MscharleyBsMaterialUiIcons.Notifications.Filled />
</Badge>
</IconButton>
</Toolbar>
</AppBar>
<Drawer
variant=`Permanent
classes=[
Paper(
Cn.make([
classes.drawerPaper,
classes.drawerPaperClose->Cn.ifTrue(!state.isOpen),
]),
),
]
open_={state.isOpen}>
<div className={classes.toolbarIcon}>
<IconButton onClick={_event => setState(Close)}>
<MscharleyBsMaterialUiIcons.ChevronLeft.Filled />
</IconButton>
</div>
<Divider />
<div> {sidebar} </div>
</Drawer>
<main className={classes.content}>
<div className={classes.appBarSpacer} />
<div>{children}</div>
</main>
</div>
}
</EnterpriseDashboardStyles>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment