diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..ca2b787 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..255bbfe --- /dev/null +++ b/.gitignore @@ -0,0 +1,77 @@ +### https://raw.github.com/github/gitignore/ea28c14da0faf75047165c10223635ba95566ad7/Node.gitignore + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..e39dc26 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Your first Polymer 3 element + +This repo goes with the [Build your first Polymer 3 element codelab](http://www.code-labs.io/codelabs/polymer-first-elements/). + +## Running the codelab + +You'll need to install some command-line tools to manage +dependencies and to run the demo. + +1. Download and install Node(recommends v8.10.0) from [https://nodejs.org/](https://nodejs.org/). Node includes the node package manager command, `npm`. + +2. Clone this repo: + + https://github.com/Polymer-Japan/polymer3-first-element.git + +3. Change directory to your local repo and install dependencies with `npm`: + + cd polymer3-first-elements + npm install + +4. To preview your element, run `npm start` from the repo directory: + + npm start + + Open `localhost:8081/components/icon-toggle/demo/` in your browser. (Note that the path uses `icon-toggle`—the + component name listed in this element's `package.json` file—rather than the actual directory name.) + +`npm start` calls `polymer serve`. +If you're wondering what `polymer serve` does, see [Run tests interactively](https://www.polymer-project.org/3.0/docs/tools/tests#run-tests-interactively) +in the Polymer docs. diff --git a/demo/index.html b/demo/index.html new file mode 100644 index 0000000..82d7619 --- /dev/null +++ b/demo/index.html @@ -0,0 +1,33 @@ + + +
+ + + +In this codelab, you'll learn how to build elements using Polymer 1.0. You'll create a simple Polymer element, a toggle button. The finished button will look something like this:
+You'll be able to use it with simple markup like this:
+<icon-toggle></icon-toggle>
+ This project introduces you to most of the key concepts in working with Polymer.
+Don't worry if you don't understand everything. Each of the concepts presented here is described in detail in the Polymer documentation.
+ + +You'll download Chrome Dev Editor and the sample code in the next step.
+If you want to use another text editor instead of Chrome Dev Editor, you'll need to download some command-line tools. See the repo for details.
+ +While you're free to use your own editor, this codelab will probably be easier to follow if you use the Chrome Dev Editor, a Chrome app IDE. If you don't have it installed yet, you can install it from the Chrome Web Store.
+ +After installing the Chrome Dev Editor, use it to clone a Git repo.
+https://github.com/googlecodelabs/polymer-first-elements.git
Note: If this is your first Chrome Dev Editor project, you may be asked to choose a folder to save projects to. You can accept the default and proceed or create and select a new directory.
+Chrome Dev Editor uses Bower to download and install a list of dependencies (including Polymer) into the bower_components/
folder.
Fetching the components may take some time if your internet connection is slow.
+When the installation finishes, your project folder should look something like this:
+The main file you'll work with is icon-toggle.html
, which contains the definition for your custom element.
To run the element demo:
+demo
folder and choose Run. (On Chrome OS, two-finger tap or ALT+click.)Chrome Dev Editor opens the demo in a new browser tab. You'll see some text where the icon toggles should appear. It doesn't look very interesting, but it shows everything is working.
+Next, you'll create a simple element that displays an icon.
+In this step, you'll learn about:
+Click on icon-toggle.html
in the root of the Chrome Dev Editor navigation panel. This file contains the skeleton of a custom element.
Unlike most HTML files, this file won't display anything if you load it in a browser—it just defines a new element. The demo you just ran imports icon-toggle.html
so it can use the <icon-toggle>
element. As you add features to the element in the following steps, they'll show up in the demo.
Start by taking a look at the existing code:
+<link rel="import"
+ href="../polymer/polymer.html">
+<link rel="import"
+ href="../iron-icon/iron-icon.html">
+ Key information:
+link rel="import"
element is an HTML import, a new way of including resources in an HTML file.iron-icon
that you'll use later in this step.Next is the definition of the element itself:
+<dom-module id="icon-toggle">
+
+ <template>
+
+ <style>
+ /* local styles go here */
+ :host {
+ display: inline-block;
+ }
+ </style>
+
+ <!-- local DOM goes here -->
+ <span>Not much here yet.</span>
+
+ </template>
+
+ Key information:
+<dom-module>
tag is an optional part of the element definition. It defines the element's internal DOM structure, or local DOM as well as the element's local styling. In this case, you're creating an element called icon-toggle
.<template>
defines the element's local DOM structure. This is where you'll add markup for your custom element.<style>
element inside the <template>
lets you define styles that are scoped to the local DOM, so they don't affect the rest of the document.:host
pseudo-class matches the custom element you're defining (in this case, the <icon-toggle>
). This is the element that contains or hosts the local DOM tree.At the end of the element definition is some JavaScript that registers the element. If the element has a <dom-module>
, this script is usually placed inside the <dom-module>
to keep everything together.
<script>
+ Polymer({
+ /* this is the element's prototype */
+ is: 'icon-toggle'
+ });
+ </script>
+</dom-module>
+ Key information:
+Polymer
call registers the element so it's recognized by the browser. is
property on the prototype is the new element's name. It has to match the id
on the <dom-module>
that contains the element's template.Now that you're familiar with the basic layout of the element, add something useful to its local DOM template.
+Find the <span>
below the local DOM goes here
comment:
<!-- local DOM goes here -->
+ <span>Not much here yet.</span>
+ </template>
+
+ Replace the <span>
and its contents with the <iron-icon>
tag below:
<!-- local DOM goes here -->
+ <iron-icon icon="polymer">
+ </iron-icon>
+ </template>
+ Key information:
+<iron-icon>
element is a custom element that renders an icon. Here it's hard-coded to use an icon named "polymer". There are a number of new CSS selectors to work with local DOM. The icon-toggle.html
file already includes a :host
selector, discussed earlier, to style the top-level <icon-toggle>
element.
To style the <iron-icon>
element, add the following CSS inside the <style>
tag after the existing content:
<style>
+ /* local styles go here */
+ :host {
+ display: inline-block;
+ }
+ iron-icon {
+ fill: rgba(0,0,0,0);
+ stroke: currentcolor;
+ }
+ :host([pressed]) iron-icon {
+ fill: currentcolor;
+ }
+ </style>
+ Key information:
+<iron-icon>
tag uses an SVG icon. The fill
and stroke
properties are SVG-specific CSS properties. They set the fill color and the outline color for the icon, respectively.:host()
function matches the host element if the selector inside the parentheses matches the host element. In this case,[pressed]
is a standard CSS attribute selector, so this rule matches when the icon-toggle
has a pressed
attribute set on it.Your custom element definition should now look like this:
+<link rel="import" href="../polymer/polymer.html">
+<link rel="import" href="../iron-icon/iron-icon.html">
+
+<dom-module id="icon-toggle">
+
+
+ <template>
+
+ <style>
+ /* local styles go here */
+ :host {
+ display: inline-block;
+ }
+ iron-icon {
+ fill: rgba(0,0,0,0);
+ stroke: currentcolor;
+ }
+ :host([pressed]) iron-icon {
+ fill: currentcolor;
+ }
+ </style>
+
+ <!-- local DOM goes here -->
+ <iron-icon icon="polymer">
+ </iron-icon>
+ </template>
+
+ <script>
+ Polymer({
+ is: 'icon-toggle',
+ });
+ </script>
+
+</dom-module>
+ Reload the demo, or re-run it by right-clicking on the demo
folder and selecting Run. You should see the toggle buttons show up with the hard-coded icon.
You'll notice that one toggle is styled as pressed, because the pressed
attribute is set in the demo. But click all you want, the button won't toggle yet; there's no code to change the pressed
property.
Right now, the element doesn't do much. In this step, you'll give it a basic API, allowing you to configure the icon from markup, using an attribute, or from JavaScript, using a property.
+First, a bit of data binding. Find the <iron-icon>
element and change the value of the icon
attribute from "polymer"
to "[[toggleIcon]]
".
<!-- local DOM goes here -->
+<iron-icon icon="[[toggleIcon]]">
+</iron-icon>
+ Key information:
+toggleIcon
is a property you'll define on the toggle button element. It doesn't have a default value yet.icon="[[toggleIcon]]"
assignment is a data binding. It links your element's toggleIcon
property with the <iron-icon>
's icon
property. You could now use your element and set the toggleIcon
property in markup or using JavaScript, as shown in the following examples. (You don't need to add this code to your project.)
<icon-toggle toggle-icon="favorite"></icon-toggle>
+ var myToggle = document.querySelector('icon-toggle');
+myToggle.toggleIcon = "favorite";
+
+ Next, add a declaration for the toggleIcon
property.
Find the script tag and add the following properties
object to the element's prototype:
<script>
+ Polymer({
+ /* this is the element's prototype */
+ is: 'icon-toggle',
+ properties: {
+ toggleIcon: String
+ }
+ });
+</script>
+</dom-module>
+ Key information:
+properties
object is a good idea if the property is going to be part of your element's public API.String
).The properties
object also supports several more features. Add the following lines to add support for the pressed
property:
Polymer({
+ /* this is the element's prototype */
+ is: 'icon-toggle',
+ properties: {
+ toggleIcon: String,
+ pressed: {
+ type: Boolean,
+ value: false,
+ notify: true,
+ reflectToAttribute: true
+ }
+ }
+ });
+ Key information:
+value
specifies the property's default value.notify
property tells Polymer to generate property change events when the property value changes. This lets the change be observed by other nodes.reflectToAttribute
property tells Polymer to update the corresponding attribute when the property changes. This lets you style the element using an attribute selector, like icon-toggle[pressed]
.Now your element has pressed
and toggleIcon
properties working.
Reload the demo, and you should see star and heart icons instead of the hard-coded icon from the previous step:
+If you're curious about where the stars and hearts come from, you can take a peek at demo/icon-toggle-demo.html
and see lines like this:
<icon-toggle toggle-icon="star"></icon-toggle>
+<icon-toggle toggle-icon="star" pressed></icon-toggle>
+
+
+ Of course, a button isn't a button if you can't click it. To toggle the button, add an event listener. To add event listeners on the host element (in this case, icon-toggle
), add a listeners
object to the element prototype:
Polymer({
+ /* this is the element's prototype */
+ is: 'icon-toggle',
+ properties: {
+ toggleIcon: String,
+ pressed: {
+ type: Boolean,
+ value: false,
+ notify: true,
+ reflectToAttribute: true
+ }
+ },
+ listeners: {
+ 'tap': 'toggle'
+ },
+ toggle: function() {
+ this.pressed = !this.pressed;
+ },
+ });
+ Key information:
+listeners
object maps event names to handler names.tap
event is generated by Polymer's gesture system when the user clicks or taps on a target with a mouse or finger.Save the icon-toggle.html
file and run the demo again. You should be able to press the button and see it toggle between its pressed and unpressed states.
You now have a button that's basically functional. But it's stuck using the existing text color for both pressed and unpressed states. What if you want to get a little flashier?
+Local DOM helps prevent users from styling your element's internals by accident. Using custom properties, your element can expose a specific set of user-styleable properties.
+You apply a custom property inside your element using the `var` function.
+background-color: var(--my-custom-property, defaultValue);
+ Where --my-custom-property is a custom property name, always starting with two dashes (--
), and defaultValue is an (optional) CSS value that's used if the custom property isn't set.
Edit your element's <style>
tag and replace the existing fill
and stroke
values with custom properties:
<style>
+ /* local styles go here */
+ :host {
+ display: inline-block;
+ }
+ iron-icon {
+ fill: var(--icon-toggle-color, rgba(0,0,0,0));
+ stroke: var(--icon-toggle-outline-color, currentcolor);
+ }
+ :host([pressed]) iron-icon {
+ fill: var(--icon-toggle-pressed-color, currentcolor);
+ }
+ </style>
+ Because of the default values, you can still style the <icon-toggle>
just by setting color
, but now you have other options. Open up demo/icon-toggle-demo.html
and set the new properties:
<style>
+ :host {
+ font-family: sans-serif;
+ --icon-toggle-color: lightgrey;
+ --icon-toggle-outline-color: black;
+ --icon-toggle-pressed-color: red;
+ };
+ </style>
+ Run the demo again to get colorful.
+That's it — you're finished. You've created an element that has a basic UI, API, and custom styling properties.
+If you have any trouble getting the element working, check out the finished version.
+ + +