Skip to content

Commit

Permalink
chore(demos): Add example themes & load theme from URL (#1967)
Browse files Browse the repository at this point in the history
To test, visit the following URL:

http://localhost:8080/theme/index.html?theme=dark

The `theme` param must be one of `baseline`, `black`, `dark`, `white`, or `yellow`.

This only works on the theme demo page; all other demo pages are unaffected.
  • Loading branch information
acdvorak committed Jan 17, 2018
1 parent 1dae53c commit f399384
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 25 deletions.
88 changes: 88 additions & 0 deletions demos/theme-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @license
* Copyright 2017 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.
*/

(function() {
var TRUSTED_THEMES = ['baseline', 'black', 'dark', 'white', 'yellow'];
var DEFAULT_THEME = TRUSTED_THEMES[0];
var PRIVATE_TYPE_MARKER = {};

/**
* A strongly-typed wrapper around a string that is guaranteed to be safe to inject into HTML.
* Inspired by the Closure library's SafeHtml class:
* https://google.github.io/closure-library/api/goog.html.SafeHtml.html
* @struct
* @final
* @private
*/
function SafeTheme() {
/** @type {!Object} */
this.PRIVATE_TYPE_MARKER_ = PRIVATE_TYPE_MARKER;

/** @type {string} */
this.PRIVATE_SAFE_VALUE_ = '';
}

/** @return {string} */
SafeTheme.prototype.toString = function() {
return 'SafeTheme{' + this.PRIVATE_SAFE_VALUE_ + '}';
};

function createSafeTheme(unsafeValue) {
var safeValue = TRUSTED_THEMES.indexOf(unsafeValue) > -1 ? unsafeValue : DEFAULT_THEME;
var safeTheme = new SafeTheme();
safeTheme.PRIVATE_SAFE_VALUE_ = safeValue;
return safeTheme;
}

/**
* @param {string} unsafeThemeName
* @return {!SafeTheme}
*/
window.getSafeDemoTheme = function(unsafeThemeName) {
return createSafeTheme(unsafeThemeName);
};

/**
* @param {string|undefined=} opt_uri
* @return {!SafeTheme}
*/
window.getSafeDemoThemeFromUri = function(opt_uri) {
return getSafeDemoTheme(parseQueryString(opt_uri).getLast('theme'));
};

/**
* @param {!SafeTheme} safeTheme
* @return {string}
*/
window.unwrapSafeDemoTheme = function(safeTheme) {
if (safeTheme instanceof SafeTheme &&
safeTheme.constructor === SafeTheme &&
safeTheme.PRIVATE_TYPE_MARKER_ === PRIVATE_TYPE_MARKER) {
return safeTheme.PRIVATE_SAFE_VALUE_;
}
console.error('Expected object of type SafeTheme, but got', safeTheme);
return 'type_error:SafeTheme';
};

/** @type {string} */
var unwrappedTheme = unwrapSafeDemoTheme(getSafeDemoThemeFromUri());

// Avoid a FOUC by injecting the stylesheet directly into the HTML stream while the browser is parsing the page.
// This causes the browser to block page rendering until the CSS has finished loading.
document.write(
'<link rel="stylesheet" href="/assets/theme/theme-' + unwrappedTheme + '.css" id="theme-stylesheet" data-hot>');
})();
7 changes: 6 additions & 1 deletion demos/theme/index.scss → demos/theme/_theme-shared.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// limitations under the License.
//

@import "../common";
@import "../../packages/mdc-button/mdc-button";
@import "../../packages/mdc-card/mdc-card";
@import "../../packages/mdc-checkbox/mdc-checkbox";
Expand All @@ -38,6 +37,7 @@
@import "../../packages/mdc-theme/mixins";
@import "../../packages/mdc-theme/variables";
@import "../../packages/mdc-toolbar/variables";
@import "../common";

// stylelint-disable selector-class-pattern, selector-max-type, scss/dollar-variable-pattern

Expand All @@ -53,6 +53,11 @@ figure {
margin-right: 0;
}

body {
color: mdc-theme-accessible-ink-color($mdc-theme-background);
background-color: $mdc-theme-background;
}

//
// Page toolbar
//
Expand Down
51 changes: 27 additions & 24 deletions demos/theme/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="/assets/theme/index.css">
<script src="/ready.js"></script>
<script src="/uri.js"></script>
<script src="/theme-loader.js"></script>
<noscript>
<link rel="stylesheet" href="/assets/theme/theme-baseline.css">
</noscript>
</head>
<body class="mdc-typography">
<header class="mdc-toolbar mdc-toolbar--fixed">
Expand Down Expand Up @@ -326,7 +330,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">

<div class="demo-checkbox-row">
<div class="mdc-form-field demo-checkbox-wrapper">
<div class="mdc-checkbox">
<div class="mdc-checkbox demo-checkbox">
<input type="checkbox"
id="enabled-checkbox"
class="mdc-checkbox__native-control"/>
Expand All @@ -345,7 +349,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
</div>

<div class="mdc-form-field demo-checkbox-wrapper">
<div class="mdc-checkbox">
<div class="mdc-checkbox demo-checkbox">
<input type="checkbox"
id="indeterminate-checkbox"
class="mdc-checkbox__native-control"/>
Expand All @@ -364,7 +368,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
</div>

<div class="mdc-form-field demo-checkbox-wrapper">
<div class="mdc-checkbox mdc-checkbox--disabled">
<div class="mdc-checkbox mdc-checkbox--disabled demo-checkbox">
<input type="checkbox"
id="disabled-checkbox"
class="mdc-checkbox__native-control"
Expand Down Expand Up @@ -542,7 +546,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">

<figure class="demo-radio-group">
<div class="mdc-form-field demo-radio-form-field">
<div class="mdc-radio">
<div class="mdc-radio demo-radio">
<input class="mdc-radio__native-control" type="radio" id="demo-radio-1" checked name="demo-radio-group-1">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
Expand All @@ -552,7 +556,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
<label id="demo-radio-1-label" for="demo-radio-1">Enabled 1</label>
</div>
<div class="mdc-form-field demo-radio-form-field">
<div class="mdc-radio">
<div class="mdc-radio demo-radio">
<input class="mdc-radio__native-control" type="radio" id="demo-radio-2" name="demo-radio-group-1">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
Expand All @@ -565,7 +569,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">

<figure class="demo-radio-group">
<div class="mdc-form-field demo-radio-form-field">
<div class="mdc-radio mdc-radio--disabled">
<div class="mdc-radio mdc-radio--disabled demo-radio">
<input class="mdc-radio__native-control" type="radio" id="demo-radio-7" disabled checked name="demo-radio-group-4">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
Expand All @@ -575,7 +579,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
<label id="demo-radio-7-label" for="demo-radio-7">Disabled 1</label>
</div>
<div class="mdc-form-field demo-radio-form-field">
<div class="mdc-radio mdc-radio--disabled">
<div class="mdc-radio mdc-radio--disabled demo-radio">
<input class="mdc-radio__native-control" type="radio" id="demo-radio-8" disabled name="demo-radio-group-4">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
Expand Down Expand Up @@ -691,16 +695,16 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
<figure>
<figcaption>Default</figcaption>

<nav class="mdc-tab-bar mdc-tab-bar--icons-with-text">
<a class="mdc-tab mdc-tab--with-icon-and-text mdc-tab--active" href="javascript:">
<nav class="mdc-tab-bar mdc-tab-bar--icons-with-text demo-tab-bar">
<a class="mdc-tab mdc-tab--with-icon-and-text mdc-tab--active demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">phone</i>
<span class="mdc-tab__icon-text">Recents</span>
</a>
<a class="mdc-tab mdc-tab--with-icon-and-text" href="javascript:">
<a class="mdc-tab mdc-tab--with-icon-and-text demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">favorite</i>
<span class="mdc-tab__icon-text">Favorites</span>
</a>
<a class="mdc-tab mdc-tab--with-icon-and-text" href="javascript:">
<a class="mdc-tab mdc-tab--with-icon-and-text demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">person_pin</i>
<span class="mdc-tab__icon-text">Nearby</span>
</a>
Expand All @@ -710,16 +714,16 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
<figure>
<figcaption>Primary Color</figcaption>

<nav class="mdc-tab-bar mdc-tab-bar--icons-with-text mdc-tab-bar--indicator-primary">
<a class="mdc-tab mdc-tab--with-icon-and-text mdc-tab--active" href="javascript:">
<nav class="mdc-tab-bar mdc-tab-bar--icons-with-text mdc-tab-bar--indicator-primary demo-tab-bar">
<a class="mdc-tab mdc-tab--with-icon-and-text mdc-tab--active demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">phone</i>
<span class="mdc-tab__icon-text">Recents</span>
</a>
<a class="mdc-tab mdc-tab--with-icon-and-text" href="javascript:">
<a class="mdc-tab mdc-tab--with-icon-and-text demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">favorite</i>
<span class="mdc-tab__icon-text">Favorites</span>
</a>
<a class="mdc-tab mdc-tab--with-icon-and-text" href="javascript:">
<a class="mdc-tab mdc-tab--with-icon-and-text demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">person_pin</i>
<span class="mdc-tab__icon-text">Nearby</span>
</a>
Expand All @@ -729,16 +733,16 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
<figure>
<figcaption>Secondary Color</figcaption>

<nav class="mdc-tab-bar mdc-tab-bar--icons-with-text mdc-tab-bar--indicator-accent">
<a class="mdc-tab mdc-tab--with-icon-and-text mdc-tab--active" href="javascript:">
<nav class="mdc-tab-bar mdc-tab-bar--icons-with-text mdc-tab-bar--indicator-accent demo-tab-bar">
<a class="mdc-tab mdc-tab--with-icon-and-text mdc-tab--active demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">phone</i>
<span class="mdc-tab__icon-text">Recents</span>
</a>
<a class="mdc-tab mdc-tab--with-icon-and-text" href="javascript:">
<a class="mdc-tab mdc-tab--with-icon-and-text demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">favorite</i>
<span class="mdc-tab__icon-text">Favorites</span>
</a>
<a class="mdc-tab mdc-tab--with-icon-and-text" href="javascript:">
<a class="mdc-tab mdc-tab--with-icon-and-text demo-tab" href="javascript:">
<i class="material-icons mdc-tab__icon" aria-hidden="true">person_pin</i>
<span class="mdc-tab__icon-text">Nearby</span>
</a>
Expand All @@ -755,7 +759,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
</h3>

<figure class="demo-text-field-wrapper">
<div class="mdc-text-field">
<div class="mdc-text-field demo-text-field">
<input type="text" class="mdc-text-field__input" id="demo-text-field-default"
name="email" aria-controls="demo-text-field-default-helper-text"
data-demo-no-js autocomplete="email">
Expand All @@ -768,7 +772,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
</p>
</figure>
<figure class="demo-text-field-wrapper">
<div class="mdc-text-field">
<div class="mdc-text-field demo-text-field">
<input type="text" class="mdc-text-field__input" id="demo-text-field-required" required
name="email" aria-controls="demo-text-field-required-helper-text"
data-demo-no-js autocomplete="email">
Expand All @@ -780,7 +784,7 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
</p>
</figure>
<figure class="demo-text-field-wrapper">
<div class="mdc-text-field mdc-text-field--box">
<div class="mdc-text-field mdc-text-field--box demo-text-field">
<input type="text" class="mdc-text-field__input" id="demo-text-field-box"
aria-controls="name-validation-message">
<label for="demo-text-field-box" class="mdc-text-field__label">With <code>--box</code> modifier</label>
Expand Down Expand Up @@ -978,7 +982,6 @@ <h3 class="mdc-typography--headline demo-component-section__heading">
var formFields = getAll('.mdc-form-field.demo-radio-form-field');
for (var i = 0, formField; formField = formFields[i]; i++) {
var formFieldInstance = new MDCFormField(formField);

var radio = formField.querySelector('.mdc-radio:not([data-demo-no-js])');
if (radio) {
var radioInstance = new MDCRadio(radio);
Expand Down
22 changes: 22 additions & 0 deletions demos/theme/theme-baseline.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Copyright 2017 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 "../../packages/mdc-theme/color-palette";

// TODO: Use theme mixin(s) instead
$demo-toolbar-progress-bar-color: secondary-light;

@import "./theme-shared";
23 changes: 23 additions & 0 deletions demos/theme/theme-black.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Copyright 2017 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 "../../packages/mdc-theme/color-palette";

// TODO: Use theme mixin(s) instead
$mdc-theme-primary: $material-color-grey-900;
$mdc-theme-secondary: $material-color-light-green-a700;

@import "./theme-shared";
58 changes: 58 additions & 0 deletions demos/theme/theme-dark.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Copyright 2017 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 "../../packages/mdc-theme/color-palette";

// TODO: Use theme mixin(s) instead
$mdc-theme-primary: $material-color-amber-300;
$mdc-theme-secondary: $material-color-pink-400;
$mdc-theme-background: $material-color-grey-900;

@import "./theme-shared";
@import "../../packages/mdc-checkbox/mixins";
@import "../../packages/mdc-radio/mixins";
@import "../../packages/mdc-textfield/mixins";

// TODO: Call theme mixins for mdc-select

.demo-checkbox {
@include mdc-checkbox-container-colors(text-secondary-on-dark);
}

.demo-radio {
@include mdc-radio-unchecked-stroke-color(text-secondary-on-dark);
}

.demo-text-field {
@include mdc-text-field-ink-color(text-primary-on-dark);
@include mdc-text-field-label-color(text-secondary-on-dark);
@include mdc-text-field-bottom-line-color(text-hint-on-dark);
}

.demo-checkbox-wrapper,
.demo-radio-form-field {
@include mdc-theme-prop(color, text-primary-on-dark);
}

.demo-tab {
@include mdc-tab-label-ink-color(text-secondary-on-dark);
@include mdc-tab-icon-ink-color(text-icon-on-dark);

&:hover {
@include mdc-tab-label-ink-color(text-primary-on-dark);
@include mdc-tab-icon-ink-color(text-secondary-on-dark);
}
}
Loading

0 comments on commit f399384

Please sign in to comment.