From f4402d90928d645ceb5f9aa6228a9cf7f681823c Mon Sep 17 00:00:00 2001 From: Conor Mack Date: Mon, 3 Feb 2020 18:38:09 +0000 Subject: [PATCH] functional md button and icon with ripple effect and classbuilder helper --- .../src/Button.svelte | 39 ------------ .../src/Button/Button.svelte | 60 +++++++++++++++++++ .../src/Button/_index.scss | 5 ++ .../src/Button/_mixins.scss | 39 ++++++++++++ .../src/Button/index.js | 2 + .../src/ClassBuilder.js | 36 ++++++++++- .../materialdesign-components/src/Icon.svelte | 13 ++++ .../materialdesign-components/src/Ripple.js | 28 +++++++++ .../src/Test/props.js | 15 ++++- .../src/Test/testComponents.js | 4 +- .../materialdesign-components/src/index.js | 4 +- 11 files changed, 199 insertions(+), 46 deletions(-) delete mode 100644 packages/materialdesign-components/src/Button.svelte create mode 100644 packages/materialdesign-components/src/Button/Button.svelte create mode 100644 packages/materialdesign-components/src/Button/_index.scss create mode 100644 packages/materialdesign-components/src/Button/_mixins.scss create mode 100644 packages/materialdesign-components/src/Button/index.js create mode 100644 packages/materialdesign-components/src/Icon.svelte create mode 100644 packages/materialdesign-components/src/Ripple.js diff --git a/packages/materialdesign-components/src/Button.svelte b/packages/materialdesign-components/src/Button.svelte deleted file mode 100644 index 1b28bf9045..0000000000 --- a/packages/materialdesign-components/src/Button.svelte +++ /dev/null @@ -1,39 +0,0 @@ - - - diff --git a/packages/materialdesign-components/src/Button/Button.svelte b/packages/materialdesign-components/src/Button/Button.svelte new file mode 100644 index 0000000000..ecef43dd29 --- /dev/null +++ b/packages/materialdesign-components/src/Button/Button.svelte @@ -0,0 +1,60 @@ + + + + + +{#if href} + + {text} + +{:else} + +{/if} diff --git a/packages/materialdesign-components/src/Button/_index.scss b/packages/materialdesign-components/src/Button/_index.scss new file mode 100644 index 0000000000..db3072f016 --- /dev/null +++ b/packages/materialdesign-components/src/Button/_index.scss @@ -0,0 +1,5 @@ +@import "@material/button/mdc-button.scss"; +@import "@material/ripple/mdc-ripple.scss"; +@import "./mixins.scss"; + +@include bbmd-button-styles(); diff --git a/packages/materialdesign-components/src/Button/_mixins.scss b/packages/materialdesign-components/src/Button/_mixins.scss new file mode 100644 index 0000000000..909be472f7 --- /dev/null +++ b/packages/materialdesign-components/src/Button/_mixins.scss @@ -0,0 +1,39 @@ +@import "@material/feature-targeting/functions"; +@import "@material/feature-targeting/mixins"; + +@mixin bbmd-button-styles($query: mdc-feature-all()) { + $feat-structure: mdc-feature-create-target($query, structure); + + .bbmd-mdc-button--size-large { + @include button-sizing($feat-structure, 21px, 40px, 15px); + } + + .bbmd-mdc-button--size-small { + @include button-sizing($feat-structure, 9px, 32px, 13px); + } + + .bbmd-mdc-button--colour-secondary { + //no feature target as relying on theme variable custom property + @include mdc-button-ink-color(secondary); + + &.mdc-button--raised, + &.mdc-button--unelevated { + @include mdc-button-container-fill-color(secondary); + @include mdc-button-ink-color(on-secondary); + } + + &.mdc-button--outlined { + @include mdc-button-outline-color(secondary); + } + } +} + +@mixin button-sizing($feat, $padding, $height, $fontSize) { + @include mdc-feature-targets($feat) { + padding: 0px $padding; + height: $height; + } + > .mdc-button__label { + font-size: $fontSize; + } +} diff --git a/packages/materialdesign-components/src/Button/index.js b/packages/materialdesign-components/src/Button/index.js new file mode 100644 index 0000000000..7b0f4b3936 --- /dev/null +++ b/packages/materialdesign-components/src/Button/index.js @@ -0,0 +1,2 @@ +import "./_index.scss"; +export { default as button } from "./Button.svelte"; diff --git a/packages/materialdesign-components/src/ClassBuilder.js b/packages/materialdesign-components/src/ClassBuilder.js index e985b6545c..f5dc8dfd3c 100644 --- a/packages/materialdesign-components/src/ClassBuilder.js +++ b/packages/materialdesign-components/src/ClassBuilder.js @@ -1,6 +1,38 @@ export default class ClassBuilder { - block = ""; - constructor(block) { + constructor(block, customDefaults) { this.block = `mdc-${block}`; + this.customDefaults = customDefaults; //will be ignored when building custom classes + } + + // classParams: {modifiers:[] (mdc), custom:[] (bbmd), extra:[] (any)} + blocks(classParams) { + let base = this.block; + if (classParams == undefined) return base; + return this.buildClass(base, classParams); + } + + //elementName: string, classParams: {} + elements(elementName, classParams) { + let base = `${this.block}__${elementName}`; + if (classParams == undefined) return base; + return this.buildClass(base, classParams); + } + + buildClass(base, classParams) { + let cls = base; + const { modifiers, customs, extras } = classParams; + if (!!modifiers) cls += modifiers.map(m => ` ${base}--${m}`).join(" "); + if (!!customs) + cls += Object.entries(customs) + .map(([property, value]) => { + //disregard falsy and values set by customDefaults constructor param + if (!!value && !this.customDefaults.includes(value)) { + //custom scss name convention = bbmd-[block | element]--[property]-[value] + return ` bbmd-${base}--${property}-${value}`; + } + }) + .join(""); + if (!!extras) cls += ` ${extras.join(" ")}`; + return cls.trim(); } } diff --git a/packages/materialdesign-components/src/Icon.svelte b/packages/materialdesign-components/src/Icon.svelte new file mode 100644 index 0000000000..2f51bd366a --- /dev/null +++ b/packages/materialdesign-components/src/Icon.svelte @@ -0,0 +1,13 @@ + + +{icon} diff --git a/packages/materialdesign-components/src/Ripple.js b/packages/materialdesign-components/src/Ripple.js new file mode 100644 index 0000000000..f16f43a1c5 --- /dev/null +++ b/packages/materialdesign-components/src/Ripple.js @@ -0,0 +1,28 @@ +import { MDCRipple } from "@material/ripple"; + +export default function ripple( + node, + props = { colour: "primary", unbounded: false } +) { + node.classList.add("mdc-ripple-surface"); + const component = new MDCRipple(node); + component.unbounded = props.unbounded; + + if (props.colour === "secondary") { + node.classList.remove("mdc-ripple-surface--primary"); + node.classList.add("mdc-ripple-surface--accent"); + } else { + node.classList.add("mdc-ripple-surface--primary"); + node.classList.remove("mdc-ripple-surface--accent"); + } + + return { + destroy() { + component.destroy(); + node.classList.remove("mdc-ripple-surface"); + node.classList.remove("mdc-ripple-surface--primary"); + node.classList.remove("mdc-ripple-surface--accent"); + component = null; + } + }; +} diff --git a/packages/materialdesign-components/src/Test/props.js b/packages/materialdesign-components/src/Test/props.js index 41f4f860a6..6aeb48f053 100644 --- a/packages/materialdesign-components/src/Test/props.js +++ b/packages/materialdesign-components/src/Test/props.js @@ -7,6 +7,19 @@ export const props = { button: { _component: "@budibase/materialdesign-components/button", _children: [], - variant: "raised" + variant: "raised", + colour: "secondary", + size: "large", + href: "", + icon: "alarm_on", + trailingIcon: true, + fullBleed: false, + text: "I am button", + disabled: false + }, + icon: { + _component: "@budibase/materialdesign-components/icon", + _children: [], + icon: "" } }; diff --git a/packages/materialdesign-components/src/Test/testComponents.js b/packages/materialdesign-components/src/Test/testComponents.js index f03e26ea78..93a304d26c 100644 --- a/packages/materialdesign-components/src/Test/testComponents.js +++ b/packages/materialdesign-components/src/Test/testComponents.js @@ -1,4 +1,4 @@ import h1 from "../H1.svelte"; -import { button } from "@BBMD"; +import { button, icon } from "@BBMD"; -export default { h1, button }; +export default { h1, button, icon }; diff --git a/packages/materialdesign-components/src/index.js b/packages/materialdesign-components/src/index.js index b714f3dfc1..c46fe38fc4 100644 --- a/packages/materialdesign-components/src/index.js +++ b/packages/materialdesign-components/src/index.js @@ -1,3 +1,3 @@ export { default as h1 } from "./H1.svelte"; -export { default as button } from "./Button.svelte"; - +export { default as icon } from "./Icon.svelte"; +export { button } from "./Button";