Showing posts with label paper-elements. Show all posts
Showing posts with label paper-elements. Show all posts

Sunday, August 24, 2014

Crazy Theming Polymer.dart with Core-Style


I am resigned.

Resigned that the core-style element and I disagree on how it should support theming. I have no illusions that I am in the right on this one, but I will attempt to make my case anyway.

When I think of custom themes, I think of setting a few colors and maybe a few other atomic values that can be used by a widget to look more or less like it fits in with the rest of the web page. In core-style, parlance, this looks like:
      <script>
        CoreStyle.g.primaryColor = 'orange';
        CoreStyle.g.secondaryColor = 'green';
      </script>
Or, alternatively, as:
        CoreStyle.g = {
          primaryColor: 'orange',
          secondaryColor = 'green'
        };
I would think that web page trying to use a widget would set these values and the widget would then honor the values. As best I can tell, this is not how core-style works.

Instead, core-style wants these global settings defined by the containing page and it wants the containing page to define styles that will apply to the internals of the widgets being used. That is, if I know that my <lorem-dialog> custom Polymer element contains paper-button elements, then core-style would have me define the global property setting and the detailed widget styling on the containing page:
  <body>
    <h1>Play Paper</h1>
    <lorem-dialog></lorem-dialog>
    <core-style id="theme">
      :host paper-button {
        border: 5px solid {{g.primaryColor}};
        padding: 2px 5px;
      }
    </core-style>
    <script>
      CoreStyle.g.primaryColor = 'orange';
    </script>
  </body>
I cannot help but think this requires the containing page to know too much about the internal implementation of the widget (the Polymer element).

I realize that I am almost certainly making a dumb assumption here. I am probably abusing this. Or not thinking right. Knowing this does not help me. Making the problem worse usually does. If I push this further—feel more pain in coding the wrong way—then I usually figure out how to do it right.

Last night, I was able to get core-style to work like I want by making the core-style's g property available in the backing Polymer element class:
Polymer('lorem-dialog', {
  created: function() {
    this.g = CoreStyle.g;
  },
  // ...
}
I could then use this reference inside the Polymer template:
<polymer-element name="lorem-dialog">
  <template>
    <!-- ... -->
    <core-style id="theme">
      :host paper-button {
        border: 5px solid {{g.primaryColor}};
        padding: 2px 5px;
      }
    </core-style>
    <core-style ref="theme"></core-style>
  </template>
  <script>
    Polymer('lorem-dialog', { /* ... */ });
  </script>
</polymer-element>
Unfortunately, that does not work in Polymer.dart. Try as I might, there is no g property on the core-style object in Dart. But…

I know that the Dart versions of the core and paper elements are just thin wrappers around the JavaScript versions. Since I know how to get the global core-style values from JavaScript, perhaps I can use Dart's awesome JavaScript interoperability to resolve this dilemma.

So, I import the dart:js packages and define a g getter to return the value of CoreStyle.g from JavaScript:
import 'package:polymer/polymer.dart';
import 'dart:html';
import 'dart:js';

@CustomTag('lorem-dialog')
class LoremDialog extends PolymerElement {
  LoremDialog.created(): super.created();

  get g => context['CoreStyle']['g'];
  // ...
}
And that actually works. In the template, I can define and reference a “theme” style that makes use of the global values from the web page using my Polymer element:
<polymer-element name="lorem-dialog">
  <template>
    <!-- ... -->
    <core-style ref="theme"></core-style>
    <core-style id="theme">
      paper-button {
        border: 5px solid {{g['secondaryColor']}};
        color: {{g['primaryColor']}};
        font-weight: bold;
        padding: 2px 5px;
      }
    </core-style>
  </template>
  <script type="application/dart" src="lorem_dialog.dart"></script>
</polymer-element>

I strongly doubt that I will offer this up as a solution in Patterns in Polymer, but it does work in both JavaScript and Dart. Even so, the amount of resistance that I get from the library is strongly suggesting to me that I am doing something wrong. But even pushing this hard against core-style is not suggesting to me the right way to tackle this problem. Hopefully something will come to me (or someone can point me in the right direction) over the course of the next few days...


Day #162

Saturday, August 23, 2014

Where Core-Style Global Properties Can Be Used


I am struggling with the core-style element in Polymer.dart. I can implement simple styles—both as a source and referencing them later. But I have not been able to get arguably the coolest feature working yet: setting group variables.

For instance, I might want to theme some elements with the primary color of orange. From the container page, I ought to be able to set this in a script like so:
    <script>
      CoreStyle.g.primaryColor = 'orange';
    </script>
Custom Polymer elements should then honor this primary color with settings like:
<link rel="import"
      href="../../../packages/polymer/polymer.html">
<!-- ... -->
<link rel="import"
      href="../../../packages/core_elements/core_style.html">
<polymer-element name="lorem-dialog">
  <template>
    <paper-button label="Bottom" on-tap="{{toggleDialog}}"></paper-button>
    <paper-button label="Center" on-tap="{{toggleDialog}}"></paper-button>
    <!-- ... -->
    <core-style id="theme">
      paper-button {
        border: 5px solid {{g.primaryColor}};
        padding: 2px 5px;
      }
    </core-style>
    <core-style ref="theme"></core-style>
  </template>
  <script type="application/dart" src="lorem_dialog.dart"></script>
</polymer-element>
Try as I might, I cannot get this to work. I wind up with an error about the g property not being defined:
Exception: Error evaluating expression 'CoreStyle.g.primaryColor': Class 'LoremDialog' has no instance getter 'CoreStyle'.

NoSuchMethodError: method not found: 'CoreStyle'
Receiver: Instance of 'LoremDialog'
Arguments: []
A hint from James Hurford suggests that including the script that sets the global color on the same page that defines the styles will work:
  <body>
    <h1>Play Paper</h1>

    <lorem-dialog></lorem-dialog>

    <core-style id="legacy-button">
      :host paper-button {
        border: 5px solid {{g.primaryColor}};
        padding: 2px 5px;
      }
    </core-style>
    <script>
      CoreStyle.g.primaryColor = 'orange';
    </script>
  </body>
In fact, that does work. My Polymer element can then reference the "legacy-button" core style and the buttons will have orange borders. But that does not seem right to me. I feel like I ought to set the global property in the main page and use it inside Polymer elements. I should not have to build the entire style in the containing page—the containing page then needs to know too much about the internals of my Polymer element. That defeats the purpose of encapsulating all of this stuff inside Polymer elements, does it not?

To decide which I right, I try it in JavaScript as well. And find… the same thing. In JavaScript, I do not get the exceptions that I do from Dart, but I still do not see global core styles inside Polymer element <core-style> elements:
<link rel="import"
      href="/bower_components/polymer/polymer.html">
<!-- ... -->
<link rel="import"
      href="/bower_components/core-style/core-style.html">
<polymer-element name="lorem-dialog">
  <template>
    <paper-button label="Bottom" on-tap="{{toggleDialog}}"></paper-button>
    <paper-button label="Center" on-tap="{{toggleDialog}}"></paper-button>
    <!-- ... -->
    <core-style id="theme">
      :host paper-button {
        border: 5px solid {{g.primaryColor}};
        padding: 2px 5px;
      }
    </core-style>
    <core-style ref="theme"></core-style>
  </template>
  <script>
    Polymer('lorem-dialog', {/* ... */});
  </script>
</polymer-element>
With this, I get 5px solid button borders, but the color is empty so the buttons default to black:



What I can do in the JavaScript version of Polymer that did not work in Polymer.dart is make a local copy of the g property in my sample Polymer class:
<link rel="import"
      href="/bower_components/polymer/polymer.html">
<!-- ... -->
<link rel="import"
      href="/bower_components/core-style/core-style.html">
<polymer-element name="lorem-dialog">
  <template><!-- ... --> </template>
  <script>
    Polymer('lorem-dialog', {
      created: function() {
        this.g = CoreStyle.g;
      },
      // ...
    });
  </script>
</polymer-element>
This makes the g property available inside the <template> of my Polymer element:



So am I wrong in my assumption that I should be able to set a global CoreStyle property in the containing page and use it inside elements? It sure seems that way. I have a workaround, but a workaround implies that I am working against the library, not using it as intended. So the question remains: am I doing something wrong or is core-style not meant to work like this?


Day #161


Thursday, August 21, 2014

Paper-Dialog-Transition (Polymer.Dart)


I love me transitions and animations, so I will take one more day to explore Paper Elements in Polymer.dart. Paper are the Polymer spin on Google's Material Design. Although I still worry that Material tends to look too Googly, I cannot argue with the concepts and emphasis that Material has for design. So I keep playing and learning.

Last night, I created a fairly simple paper-dialog in Polymer.dart:



What is most remarkable to me is that it worked so easily. A while back, I could barely get Core Elements to work in Polymer.dart. Now both Core and Paper work with ease. The team behind Polymer.dart has been active (thanks folks!).

One of the core tenants of Material Design is “Motion with meaning.” There is no motion in my paper-dialog and I can't help but notice that there is a paper-dialog-transition element. As if that were not enough to tempt me to investigate, the documentation on the JavaScript version is currently empty. So really, I can't help but investigate.

My first thought is to use the <paper-dialog-transition> as a drop-in replacement for last night's <paper-dialog>:
    <paper-dialog-transition heading="Title for dialog" opened="true">
      <p>Lorem ipsum ....</p>
      <p>Id qui scripta ...</p>
      <paper-button label="More Info..." dismissive></paper-button>
      <paper-button label="Decline" affirmative></paper-button>
      <paper-button label="Accept" affirmative autofocus></paper-button>
    </paper-dialog-transition>
I also add the appropriate <link> import of paper-dialog-transition to the document <head>:
  <head>
    <!-- Load platform polyfills -->
    <script src="packages/web_components/platform.js"></script>
    <script src="packages/web_components/dart_support.js"></script>

    <!-- Load component(s) -->
    <link rel="import"
          href="packages/paper_elements/paper_dialog_transition.html">
    <link rel="import"
          href="packages/paper_elements/paper_dialog.html">
    <link rel="import"
          href="packages/paper_elements/paper_button.html">

    <!-- Start Polymer -->
    <script type="application/dart">
      export 'package:polymer/init.dart';
    </script>
  </head>
But that does not work. Loading the page, I do not find a pre-opened dialog. I find nothing. If I query in Chrome's JavaScript console for paper-dialog-transition elements, I find not one, but three:



The first two are added by the Polymer transformer in Dart Pub. The last is the real one that I added. And it does not behave like a <polymer-dialog>:
> d = document.querySelectorAll('paper-dialog-transition')[2]
    <paper-dialog-transition heading=​"Title for dialog" opened=​"true" hidden>​…​</paper-dialog-transition>​
> d.toggle()
    TypeError: undefined is not a function
It seems like <paper-dialog-transition> is adding those first two copies of itself for a reason. I am ultimately unable to figure out why by reading the code. Or rather, I stumble across some demo code that seems to do use the element.

The key seems to be using the IDs of the extra <paper-dialog-transition> elements in transition attributes of <paper-dialog> elements:
    <paper-dialog heading="Bottom Dialog" transition="paper-dialog-transition-bottom">
      <p>Lorem ipsum ....</p>
      <p>Id qui scripta ...</p>
      <paper-button label="More Info..." dismissive></paper-button>
      <paper-button label="Decline" affirmative></paper-button>
      <paper-button label="Accept" affirmative autofocus></paper-button>
    </paper-dialog>
I add a similar <paper-dialog> with a transition of paper-dialog-transition-center.

The final piece of the puzzle comes directly from the demo page. I add two buttons that invoked the toggle() method on the appropriate dialog:
    <paper-button label="Bottom" onclick="toggleDialog('paper-dialog-transition-bottom')"></paper-button>
    <paper-button label="Center" onclick="toggleDialog('paper-dialog-transition-center')"></paper-button>
  <script>
    function toggleDialog(transition) {
      var dialog = document.
        querySelector('paper-dialog[transition=' + transition + ']');
      dialog.toggle();
    }
  </script>
The result is quite pleasing pair of dialog transitions. One travels up from the bottom of the page, the other expands from the center:


The <paper-dialog-transition> transitions work nicely with the <paper-dialog> elements. Toggling the dialog, clicking elsewhere, clicking decline, even hitting escape all result in the transition reversing itself. All I needed to make this work was the addition of transition attributes and the import of paper_dialog_transition.html (removing this import disables the transitions).

I have to admit that the usage of <paper-dialog-transition> was not obvious to me. By extension, I have the feeling that I am a little weak on core-transition-css (which paper-dialog-transition extends). This may be worth a little more exploration. Tomorrow.

Day #159

Wednesday, August 20, 2014

Paper Dialogs with Polymer.dart


I have quite enjoyed playing with Material Design concepts of late. Interestingly, the solutions that I wound up with, both in Dart and JavaScript, rely on Core Elements instead of the material design based Paper Elements.

Rather than try to cram Paper into my very pretty Core/Material design in <x-pizza>, I start a completely new project. In this case, I am going to try to use the Paper elements directly in a Dart application rather than pulling them into another Polymer.dart element.

I start this “paper” application with a pubspec.yaml of:
name: paper
dependencies:
  polymer: any
  paper_elements: any
transformers:
- polymer
I had originally thought to skip the polymer dependency, but without it, pub serve will complain:
$ pub serve
Error in pubspec for package "paper" loaded from pubspec.yaml:
"transformers.polymer" refers to a package that's not a dependency
And if I try to remove the transformer, Dartium complains:
Exception: Missing initialization of polymer elements. Please check that the list of entry points in your pubspec.yaml is correct. If you are using pub-serve, you may need to restart it.
So, even though I am not creating my own Polymer elements, I need Polymer listed as a dependency. I also have to initialize the page in the same manner—the index.html file looks like:
<!DOCTYPE html>
<html>
  <head>
    <title>Play Paper</title>

    <!-- Load platform polyfills -->
    <script src="packages/web_components/platform.js"></script>
    <script src="packages/web_components/dart_support.js"></script>

    <!-- Load component(s) -->
    <link rel="import"
          href="packages/paper_elements/paper_dialog.html">
    <link rel="import"
          href="packages/paper_elements/paper_button.html">

    <!-- Start Polymer -->
    <script type="application/dart">
      export 'package:polymer/init.dart';
    </script>

  </head>
  <body>
    <h1>Play Paper</h1>
    <!-- ... -->
  </body>
</html>
That is the same setup that I use with any Polymer code—the same way to load the JavaScript polyfills (and associated Dart support), the same way to import the elements (Paper in this case) and the same way to start the Polymer.dart system.

As the elements being imported suggest, I am going to try a simple Paper dialog in Dart, so I add the following custom HTML (copied from the JavaScript documentation):
    <paper-dialog heading="Title for dialog" opened="true">
      <p>Lorem ipsum ....</p>
      <p>Id qui scripta ...</p>
      <paper-button label="More Info..." dismissive></paper-button>
      <paper-button label="Decline" affirmative></paper-button>
      <paper-button label="Accept" affirmative autofocus></paper-button>
    </paper-dialog>
The trick with this simple, open-by-default dialog is the opened attribute on <paper-dialog>. This would normally be accomplished via the toggle() method on <paper-dialog> elements, but I am simply hoping to verify that Paper works tonight. I will worry about additional interaction another night.

After starting the pub serve test web server, I find:



That was easy! But it certainly could look better—especially when I compare it to everything else in Material design. To get somewhat better results, I follow along with the Material Typography guide, and switch to the Roboto font:
  <head>
    <title>Play Paper</title>
    <link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
    <style>
      html { font-family: 'Roboto', sans-serif; }

      paper-button[autofocus] {
        color: #4285f4;
      }

      paper-button:hover {
        color: #2a56c6;
      }

      paper-button::shadow #ripple {
        color: #2a56c6;
      }
    </style>

    <!-- Load platform polyfills -->
    <!-- ... -->
  </head>
This winds up looking very Googly (and much better):



So Paper in Dart turns out to be blessedly easy—at least getting started with it. I may add some interaction or play with some additional elements tomorrow. Or, if that turns out to be too easy (because Polymer.dart works too darn well), then I may move on to other topics.


Day #158

Monday, August 18, 2014

Core Elements in Polymer.Dart (Including Animation)


When last I tried to use Core Element in Polymer.dart, it did not go well. I know the team has made incredible progress in the interim, so tonight seems an opportune time to find out how much better things are.

The Core and Paper element work that I have been building in the JavaScript <x-pizza> element should be a solid test. I start, as I do with all Dart projects, with a pubspec.yaml including Paper and Core dependencies:
name: paper
dependencies:
  polymer: any
  core_elements: any
  paper_elements: any
dev_dependencies:
  scheduled_test: any
transformers:
- polymer:
    entry_points:
    - web/index.html
    - test/index.html
A quick pub install and I should be good to go:
$ pub install
Resolving dependencies... (1.4s)
Downloading paper_elements 0.1.0+3...
Downloading core_elements 0.1.1+2...
Downloading quiver 0.18.2...
Got dependencies!
I add some initial core imports and elements to the <x-pizza-toppings> child element:
<link rel="import" href="../../../packages/polymer/polymer.html">
<link rel="import" href="../../../packages/core_elements/core_icons.html">
<link rel="import" href="../../../packages/core_elements/core_item.html">
<polymer-element name="x-pizza-toppings">
  <template>
    <!-- ... -->
    <template repeat="{{ingredient in ingredients}}">
      <core-item
         icon="add-circle"
         label="{{ingredient}}"
         on-tap="{{add}}">
      </core-item>
    </template>
    <!-- ... -->
  </template>
  <script type="application/dart" src="x_pizza_toppings.dart"></script>
</polymer-element>
And, when I load things up in Dartium, I get a nice old failure:
Exception: Unsupported operation: Couldn't find `document._registerDartTypeUpgrader`. Please make sure that `packages/web_components/dart_support.js` is loaded and available before calling this function.
Aha! So that is what the dart_support.js script does. I know that it is included in generated Polymer.dart code, but until now I could find no use for it.

To resolve, I add this back in the main index.html page that pulls in Polymer, the Polymer elements that I want to use, and initializes everything:
<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Load platform polyfills -->
    <script src="packages/web_components/platform.js"></script>
    <script src="packages/web_components/dart_support.js"></script>

    <!-- Load component(s) -->
    <link rel="import"
          href="packages/paper/elements/x-pizza.html">

    <!-- Start Polymer -->
    <script type="application/dart">
      export 'package:polymer/init.dart';
    </script>
  </head>
  <body>
    <div class="container">
      <h1>Ye Olde Dart Pizza Shoppe</h1>
      <x-pizza></x-pizza>
    </div>
  </body>
</html>
That aside, there are relatively few issues that I have to address converting the <x-pizza> element from JavaScript to Dart. I have to mark properties that are used in the template as @observable. I believe that was the only “significant” change required and it was not that difficult.

I do note that I can omit the import of transitions (i.e. the hero transition code). When I was building the JavaScript version, I thought this was required, but even after double checking the JavaScript version, it seems that importing the core-animated-pages is sufficient to pull in the transition code, which makes sense:
<link rel="import" href="../../../packages/polymer/polymer.html">
<link rel="import" href="../../../packages/core_elements/core_icon.html">
<link rel="import" href="../../../packages/core_elements/core_pages.html">
<link rel="import" href="../../../packages/core_elements/core_animated_pages.html">
<link rel="import" href="x-pizza-toppings.html">
<polymer-element name="x-pizza">
  <template>
    <!-- ... -->
    <core-animated-pages selected="{{toppingsListOrDetails}}" transitions="hero-transition">
      <!-- ... -->
    </core-animated-pages>
  </template>
  <script type="application/dart" src="x_pizza.dart"></script>
</polymer-element>
And all of this works just fine:


Well, maybe 100% fine. I need to figure out why the icons are the wrong size. There is also a flash of unstyled CSS when the page first loads. I think this would not be an issue if the element were compiled to JavaScript (instead of loaded via pub serve)—something to check another day. Those minor issues aside, this worked pretty well. Infinitely better than the last time I tried it!


Day #156

Saturday, August 16, 2014

Don't Mix Browser Events in Paper (Polymer)


I could play with Paper Elements and the benefits of adhering to Material Design all day:


But the gods of my chain and Jerry Seinfeld would frown on that, so...

Up today, I worry about event propagation in paper elements. Specifically, when I add a pizza topping to an <x-pizza>, the event should not trigger the pizza toppings card to transition back to its initial “chip” state:


The ingredients are core-item elements whose on-click attribute points to the add() method on the backing class:
<polymer-element name="x-pizza-toppings" attributes="ingredients name">
  <template>
    <!-- ... -->
    <template repeat="{{ingredient in ingredients}}">
      <core-item
         scale-up
         id="{{class_name(ingredient)}}"
         icon="add-circle"
         label="{{ingredient}}"
         on-click="{{add}}">
      </core-item>
    </template>
  </template>
  <script src="x_pizza_toppings.js"></script>
</polymer-element>
So my first thought is to prevent event propagation in every conceivable way:
Polymer('x-pizza-toppings', {
  // ...
  add: function(e) {
    this.model.push(e.target.label);

    console.log('stop the events!')
    e.preventDefault();
    e.stopPropagation();
    e.stopImmediatePropagation();
    return false;
  },
  // ...
});
Unfortunately, it seems that my first instinct is incorrect. Actually, it turns out to be just a little off.

The transition between chip and card (and back) are both listening for tap events, not click events:
    <core-animated-pages selected="{{toppingsListOrDetails}}" transitions="hero-transition">
      <section>
        <div class="chip-container"
             hero
             on-tap="{{transitionTopping}}">
          <!-- Chips go here... -->
        </div>
      </section>
      <section>
        <div class="card" layout horizontal
             hero-id="{{selectedTopping}}"
             hero
             on-tap="{{transitionTopping}}">
          <!-- Chips go here... -->
        </div>
      </section>
    </core-animated-pages>
Tap events are one the Polymer normalized events, which aid in cross-platform and cross-device development. The tap event work for both mouse and finger, firing when the pointer goes down and up again.

So the fix is to adopt a consistent approach to events:
    <template repeat="{{ingredient in ingredients}}">
      <core-item
         scale-up
         id="{{class_name(ingredient)}}"
         icon="add-circle"
         label="{{ingredient}}"
         on-tap="{{add}}">
      </core-item>
    </template>
And then, I can reduce the event propagation to simply stopPropagation():
Polymer('x-pizza-toppings', {
  // ...
  add: function(e) {
    this.model.push(e.target.label);
    e.stopPropagation();
  },
  // ...
});
With that, I can add toppings to my heart's content:


It only makes sense to be consistent with events. This is, after all, the point of normalizing some of the events in Polymer. Hopefully I will take this lesson to heart in the future.


Day #154

Friday, August 15, 2014

Don't Absolute Position in the Hero Transition


The “hero” transition between chip and card is great fun:


I enjoyed exploring it (and the source demo from which I borrowed liberally). I enjoyed playing with the chip and cards even more. With relatively little effort, I was able to create what Material Design refers to as “motion with meaning.”

But there are at least two issues that I noted after an hour or so of clicking (it really is that fun). First, the hero transition of the half-a-pizza icon is not as smooth as it might be—especially at the end where it jumps down. Second, when I click a pizza topping to add to the pizza, the click event continues to propagate until the card closes.

I like to pick the harder battles first (even though the easy battles never seem to be easy). In this case, I guess that I can simply stop normal event propagation to solve the second problem. Since this Material and Paper Elements stuff is still new to me, that seems the harder of the two problems, so I start with that.

My furst guess is that the problem is related to nesting three elements. In my chip, I am “hero-ing” the entire chip, the top of the chip that contains the color, and the icon:



The hero IDs indicated tell Paper elements how to map visual elements during transitions. The card ultimately gets assigned matching hero IDs like so:



The first_half hero ID maps the chip for first half pizza toppings to the card for first half toppings. The icon in the chip maps to the icon in the card, which only seems to make sense. It is a little odd that I mapped the yellow top in the chip to the <div> that holds the icon in the card. Perhaps that is where things are going wrong?

I think I had meant to map the yellow chip top to the yellow card side. So I move the “color” hero ID to the side in the card:
        <div class="card" layout horizontal
             hero-id="{{selectedTopping}}"
             hero
             on-tap="{{transitionTopping}}">
          <div class="card-left"
               style="background:{{selectedColor}};"
               hero-id="{{ selectedTopping }}-color"
               hero></div>
          <div class="card-right" flex>
            <div layout horizontal center>
              <div class="card-icon"
                   style="background-color:{{selectedColor}};">
                <core-icon
                   src="/assets/{{selectedTopping}}.svg"
                   hero-id="{{ selectedTopping }}-icon"
                   hero></core-icon>
              </div>
              <div flex>
                <!-- Topping selectors here...
              </div>
            </div>
          </div>
        </div>
The effect is actually more satisfying than the previous hero ID mapping:


But the the icon transition back is no better.

After much further fiddling, I find that I can get everything fixed if I simply remove absolute positioning on the elements being transitioned. I really wish this had occurred to me earlier because it took me a lot of digging. On the plus side, I got to use the break-on-element-attribute change in Chrome inspector.

In retrospect, maybe this should have occurred to me sooner as the chip icon was the only thing being absolutely positioned:
      .chip-top .chip-icon {
        position: absolute;
        left: 50px;
        bottom: 75px;
      }
The fix is to simply change that to use plain old padding:
      .chip-top .chip-icon {
        padding-top: 60px;
        padding-left: 50px;
      }
With that, I get a beautiful transition back and forth between chip and card:


So the moral of the story is don't use absolute position on hero (and possibly any) transitions in Paper elements. Also, don't use yellow for the demo color -- it can be a little hard to see, but hard to change when you don't notice until you're nearly done with a post (sorry about that).

Up tomorrow: I try to solve the “easy” problem (please let it be easy).


Day #153


Wednesday, August 13, 2014

Custom SVGs for Core-Icons in Polymer


After last night, I have a nicely animated <x-pizza> Polymer element:


The animation and elements come courtesy of Paper and Core elements from the Polymer project. I think they work together well to be bold & graphics as well as providing motion with meaning (core tenants of Material Design).

Perhaps the most exciting aspect of all of this is that I used no code. The <x-pizza> element still has code logic for building pizzas, but the tabs and motion of the pages movings comes entirely from Polymer elements:
    <paper-tabs selected="0" id="tabs">
      <paper-tab id="first_half_tab">
        <p>First Half Toppings</p>
      </paper-tab>
      <paper-tab id="second_half_tab">
        <p>Second Half Toppings</p>
      </paper-tab>
      <paper-tab id="whole_tab">
        <p>Whole Pizza Toppings</p>
      </paper-tab>
    </paper-tabs>
    <core-animated-pages selected="{{$.tabs.selected}}" transitions="slide-from-right">
      <x-pizza-toppings id="firstHalfToppings"
                        name="First Half Toppings"
                        ingredients="{{ingredients}}"></x-pizza-toppings>
      <x-pizza-toppings id="secondHalfToppings"
                        name="Second Half Toppings"
                        ingredients="{{ingredients}}"></x-pizza-toppings>
      <x-pizza-toppings id="wholeToppings"
                        name="Whole Toppings"
                        ingredients="{{ingredients}}"></x-pizza-toppings>
    </core-animated-pages>
As cool as that is, I think it might only be a start. There may be more appropriate material metaphors that fit what I am trying to accomplish with the pizza toppings. Toward that end, I would like to start with a “chip” and page approach similar to the music example in the core-animated-page demos.

Chips, in Material, are small items that represent a more complex entity. In this case, I might represent the relatively complex pizza toppings chooser with a simple chip. I will worry about expanding the chip tomorrow, for now I shamelessly copy the HTML from the demo into my <x-pizza> element, making a few hand edits. This leaves me with:
      <section>
        <div class="chip-container">
          <div class="chip">
            <div class="chip-top" style="background:#f4db33;">
              <div class="chip-icon">
                <core-icon src="/assets/first_half.svg"></core-icon>
              </div>
            </div>
            <div class="chip-bottom">
              <div class="chip-title">Add First Half Toppings</div>
            </div>
          </div>
          <!-- Other topping chips here ... -->
        </div>
      </section>
I also pull in the chip CSS from the demo page, again making a few hand-edits for my chip-icon and chip-title CSS classes. All of this copying and pasting of HTML and CSS feels like the early days of the internet all over. It might be nice to have a chip Polymer element at some point, but this will work for tonight.

With that, I create a couple of SVGs. There are no icons in the default iconset from Polymer that look like pizza halves, so I have to create my own. Fortunately, I am a pro at doing this in Inkscape by now. Armed with whole.svg, first_half.svg, and second_half.svg, my <core-icon> element can now pull from the proper src location and... voilà!



Of course, I still need to hook those up to actual pages, but hopefully that will not be too hard (I will find out tomorrow). I am not 100% sold on the look of these elements. It begins to look a little too much like Google property. Still, I have to admit that it is better than the tabs (which also looked a bit Googlish). If everyone adopts this style, then the web will wind up a very boring place. Then again, I suppose Boostrap is doing that already (to an extent) and this has the advantage of baking in “motion with meaning,” which I will explore tomorrow.

And if I am very lucky, I will finally figure out what a “hero” transition is...


Day #151