Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
refactor(fab): Move all FAB styles to mixins (#1166)
Browse files Browse the repository at this point in the history
Partially resolves #1143.

New `mdc-fab-theme` mixin uses a config object. This makes it easy to extend somebody else's theme values, overriding only specific properties or sub-properties.
  • Loading branch information
acdvorak authored Aug 25, 2017
1 parent 90e7556 commit bb61827
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 91 deletions.
31 changes: 23 additions & 8 deletions packages/mdc-fab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,36 @@ You can also do this declaratively when using the [material-components-web](../m

FABs are fully aware of ripple styles, so no DOM or CSS changes are required to use them.

## Classes

### Block

The block class is `mdc-fab`. This defines the top-level button element.

### Element

The button component has a single `span` element added as a child of the button due to buttons not adhering to flexbox rules
in all browsers. See [this Stackoverflow post](http://stackoverflow.com/posts/35466231/revisions) for details.

### Modifier
### CSS Classes

CSS Class | Description
--- | ---
`mdc-fab--mini` | Makes the fab smaller (40 x 40 pixels)
`mdc-fab--plain` | Makes the FAB have a white background

### Sass Mixins

#### `mdc-fab-theme($config)`

Generates theme-related CSS properties from the given config map.

All properties are optional. Properties that are `null` or unspecified will not be emitted.

The provided modifiers are:
Property | Description
--- | ---
`bg-color` | Background color of the FAB
`fg-color` | Foreground color of the icon
`ripple-config` | Ripple configuration (see the [mdc-ripple README][ripple-readme])
`tap-highlight-color` | Color of the [`-webkit-tap-highlight-color`][tap-highlight] property (for mobile devices that support it)

| Class | Description |
| ------------------| --------------------------------------- |
| `mdc-fab--mini` | Makes the fab smaller (40 x 40 pixels). |
| `mdc-fab--plain` | Makes the FAB have a white background. |
[ripple-readme]: https://github.com/material-components/material-components-web/blob/master/packages/mdc-ripple/README.md
[tap-highlight]: https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-tap-highlight-color
123 changes: 123 additions & 0 deletions packages/mdc-fab/_mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@import "@material/animation/functions";
@import "@material/animation/variables";
@import "@material/elevation/mixins";
@import "@material/ripple/mixins";
@import "@material/theme/mixins";

@mixin mdc-fab-base_ {
@include mdc-ripple-base;

display: inline-flex;
position: relative;
justify-content: center;
width: 56px;
height: 56px;
padding: 0;
transition: box-shadow 280ms $mdc-animation-standard-curve-timing-function;
border: none;
border-radius: 50%;
cursor: pointer;
user-select: none;
box-sizing: border-box;
fill: currentColor;
-moz-appearance: none;
-webkit-appearance: none;
overflow: hidden;

@include mdc-elevation(6);

&:active {
@include mdc-elevation(12);
}

&:active,
&:focus {
// TODO(acdvorak): Should this be paired with states and/or ripple? We don't want to disable outline
// (an accessibility/usability feature) unless we're confident that there is also a visual indication that the
// element has focus. If the client has customized the DOM in some unexpected way, and is certain that another
// element will receive focus instead, they can always override this property manually in their CSS.
outline: none;
}

&:hover {
cursor: pointer;
}

&::-moz-focus-inner {
padding: 0;
border: 0;
}

// This allows for using SVGs within them to align properly in all browsers.
// Can remove once: https://bugzilla.mozilla.org/show_bug.cgi?id=1294515 is resolved.

// stylelint-disable selector-max-type
// postcss-bem-linter: ignore
> svg {
width: 100%;
}
// stylelint-enable selector-max-type
}

@mixin mdc-fab--mini_ {
width: 40px;
height: 40px;
}

@mixin mdc-fab--disabled_ {
cursor: default;
pointer-events: none;
}

@mixin mdc-fab-theme($config) {
$bg-color: map-get($config, bg-color);
$fg-color: map-get($config, fg-color);
$ripple-config: map-get($config, ripple-config);
$tap-highlight-color: map-get($config, tap-highlight-color);

@if $ripple-config {
@include mdc-ripple-bg(map-merge((pseudo: "::before"), $ripple-config));
@include mdc-ripple-fg(map-merge((pseudo: "::after"), $ripple-config));
}

@if $tap-highlight-color {
@if $ripple-config {
&:not(.mdc-ripple-upgraded) {
@include mdc-theme-prop(-webkit-tap-highlight-color, $tap-highlight-color);
}
}

@else {
@include mdc-theme-prop(-webkit-tap-highlight-color, $tap-highlight-color);
}
}

@if $bg-color {
@include mdc-theme-prop(background-color, $bg-color);
}

@if $fg-color {
@include mdc-theme-prop(color, $fg-color);
}
}

@mixin mdc-fab__icon_ {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
113 changes: 30 additions & 83 deletions packages/mdc-fab/mdc-fab.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,98 +12,45 @@
// See the License for the specific language governing permissions and
// limitations under the License.

@import "@material/animation/functions";
@import "@material/animation/variables";
@import "@material/elevation/mixins";
@import "@material/ripple/mixins";
@import "@material/theme/mixins";
@import "@material/theme/variables";
@import "./mixins";

// postcss-bem-linter: define fab
.mdc-fab {
@include mdc-ripple-base;
@include mdc-ripple-bg((pseudo: "::before", base-color: white, opacity: .16));
@include mdc-ripple-fg((pseudo: "::after", base-color: white, opacity: .16));

display: inline-flex;
position: relative;
justify-content: center;
width: 56px;
height: 56px;
padding: 0;
transition: box-shadow 280ms $mdc-animation-standard-curve-timing-function;
border: none;
border-radius: 50%;
cursor: pointer;
user-select: none;
box-sizing: border-box;
fill: currentColor;
-moz-appearance: none;
-webkit-appearance: none;
overflow: hidden;

&:not(.mdc-ripple-upgraded) {
-webkit-tap-highlight-color: rgba(0, 0, 0, .18);
}

@include mdc-theme-prop(background-color, secondary);
@include mdc-theme-prop(color, text-primary-on-secondary);
@include mdc-elevation(6);

&--mini {
width: 40px;
height: 40px;
}

&--plain {
background-color: white;

@include mdc-theme-prop(color, text-primary-on-light);
@include mdc-ripple-bg((pseudo: "::before"));
@include mdc-ripple-fg((pseudo: "::after"));
}

&:active,
&:focus {
outline: none;
}

&:active {
@include mdc-elevation(12);
}

&:hover {
cursor: pointer;
}

&::-moz-focus-inner {
padding: 0;
border: 0;
.mdc-fab {
@include mdc-fab-base_;
@include mdc-fab-theme((
bg-color: secondary,
fg-color: text-primary-on-secondary,
ripple-config: (base-color: white, opacity: .16),
tap-highlight-color: rgba(0, 0, 0, .18)
));

:disabled &,
&:disabled {
@include mdc-fab-theme((
bg-color: rgba(0, 0, 0, .12),
fg-color: rgba(0, 0, 0, .26)
));
@include mdc-fab--disabled_;
}
}

// This allows for using SVGs within them to align properly in all browsers.
// Can remove once: https://bugzilla.mozilla.org/show_bug.cgi?id=1294515 is resolved.

// stylelint-disable selector-max-type
// postcss-bem-linter: ignore
> svg {
width: 100%;
}
// stylelint-enable selector-max-type
.mdc-fab--plain {
@include mdc-fab-theme((
bg-color: white,
fg-color: text-primary-on-light,
ripple-config: (),
tap-highlight-color: rgba(0, 0, 0, .18)
));
}

fieldset:disabled &,
&:disabled {
background-color: rgba(0, 0, 0, .12);
color: rgba(0, 0, 0, .26);
cursor: default;
pointer-events: none;
}
.mdc-fab--mini {
@include mdc-fab--mini_;
}

.mdc-fab__icon {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
@include mdc-fab__icon_;
}

// postcss-bem-linter: end

0 comments on commit bb61827

Please sign in to comment.