SlideShare una empresa de Scribd logo
1 de 245
SF Angular Meetup
OpenTable
January 12, 2015
MTV Angular Meetup
Google
January 20, 2015
Reusable Components
in Angular
Architecture, transclusion, and more
Who We Are
Rachael L Moore
UI Engineer
morewry
Kara Erickson
Web Engineer
kara
UI Components
What they are
Why to use them
How to design them
Implementations
Multi-transclude
Attribute passing
Sub-directives
Dropdown isolate scopes
16:9 video tour of our app UI
UI Components
What and why
FIREWORKS NIGHT CC BY GPS
Benefits of UI ComponentsBenefits of UI Components
ConsistencyConsistency
Faster DevelopmentFaster Development
QualityQuality
TROPHIES CC BY BRAD K
Better DeploymentBetter Deployment
T-10 D PARACHUTE CC BY PROGRAM EXECUTIVE OFFICE
AccuracyAccuracy
Better FocusBetter Focus
FIREWORKS NIGHT CC BY GPS
Consistency
Faster development
Quality
Benefits of UI Components
Better deployment
Accuracy
Better focus
Custom ElementsCustom Elements
BRIGHT YELLOW FLOWER CROCHET CC BY SINGLE STITCHES
Coherent ConfigCoherent Config
FIREWORKS NIGHT CC BY GPS
Consistency
Faster development
Quality
Better deployment
Accuracy
Better focus
Custom elements
Coherent config
Benefits of UI Components
How to Design UI ComponentsHow to Design UI Components
Prior ArtPrior Art
Natural LanguageNatural Language
VolatilityVolatility
ETNA VOLCANO PAROXYSMAL ERUPTION CC BY GNUCKX
Prior art
Natural language
Volatility
How To Design UI Components
Visual patterns
Behavioral patterns
Technical limitations
Site Layout
Design
Visual PatternsVisual Patterns
SANTAS AT CITY HALL CC BY DENNIS YANG
Technical LimitationsTechnical Limitations
FACEPALM CC BY BRANDON GRASLEY
<div>
<header />
<nav />
<div />
<footer />
</div>
<directive-1>
<directive-2 />
<directive-3 />
<directive-4 />
<directive-5 />
</directive-1>
<self /> closing tags for brevity only (do not try at home)
<ot-site>
</ot-site>
site.html
Site Layout
Implementation
Body 
Menu 
Header 
<ot-site></ot-site>
HTML attributes
Transclusion
Site scaffold interface
<ot-site current-app=
"Marketing">
</ot-site>
HTML attributes
Transclusion
Site scaffold interface
<ot-site current-app="
Marketing" license="
2015 Copyright
OpenTable" account-
manager="true" app-
switcher="false" site-
title="AppCtrl.title"
sidebar="See more
options" card-layout="
true" card-values="
AppCtrl.cards"></ot-
site>
HTML attributes
Transclusion
Site scaffold interface
Marketing"license="
2015CopyrightOpenTable"
account-manager="true"
app-switcher="false"
site-title="AppCtrl.
title"sidebar="
Seemoreoptions"sidebar-
items="AppCtrl.
sidebarList"card-
layout="true"card-
values="AppCtrl.cards"
rid-selector="true"
dropdown-theme="dark"
sub-header="Themes"
tabs="true"tabs-items="
AppCtrl.tabs"sidebar-
content="Here is very
HTML attributes
Transclusion
Site scaffold interface
<ot-site>
<div>Transcluded
content</div>
</ot-site>
HTML attributes
Transclusion
Site scaffold interface
<ot-site>
</ot-site>
index.html
<ot-site>
<div>
I’m transcluding the header.
</div>
<div>
I’m transcluding the menu.
</div>
<div>
I’m transcluding the body.
</div>
</ot-site>
index.html
Body 
Menu 
Header 
.directive("otSite", () => {
return {
scope: {},
template: ``
};
});
site.js
.directive("otSite", () => {
return {
scope: {},
template: `` .
};
});
site.js
Quick ES6 Review
"<div>" +
"<p ng-class="{'highlight': selected}">Hey</p>" +
"</div>"
`<div>
<p ng-class="{'highlight': selected}">Hey</p>
</div>`
ES6 Review - Template Strings
ES5
ES6
ES6 Review - Template Strings
var name = "Kara";
var greeting = "Hi " + name + "!"; // Hi Kara!
var name = "Kara";
var greeting = `Hi ${name}!`; // Hi Kara!
ES5
ES6
ES6 Review - Fat Arrows
var that = this;
var toggleMenu = function() {
that.menuShowing = !that.menuShowing;
};
var toggleMenu = () => {
this.menuShowing = !this.menuShowing;
};
ES5
ES6
.directive("otSite", () => {
return {
scope: {},
template: ``
};
});
site.js
.directive("otSite", () => {
return {
scope: {},
template: `
<div>
<header></header>
<nav></nav>
<main></main>
<footer></footer>
</div>`
};
site.js
...
Code Demo
<ot-site>
<div>
I’m transcluding the header.
</div>
<div>
I’m transcluding the menu.
</div>
<div>
I’m transcluding the body.
</div>
</ot-site>
index.html
.directive("otSite", () => {
return {
scope: {},
template: `
<div>
<header></header>
<nav></nav>
<main></main>
<footer></main>
</div>`
};
site.js
...
.directive("otSite", () => {
return {
scope: {},
transclude: true,
template: `
<div>
<header></header>
<nav></nav>
<main></main>
<footer></main>
</div>`
site.js
...
.directive("otSite", () => {
return {
scope: {},
transclude: true,
template: `
<div>
<header ng-transclude></header>
<nav ng-transclude></nav>
<main ng-transclude></main>
<footer></footer>
</div>`
site.js
...
Code Demo
$transclude((function(clone)) {
$element.empty();
$element.append(clone);
});
}
ng-transclude
.directive("otSite", () => {
return {
scope: {},
transclude: true,
template: `
<div>
<header ng-transclude></header>
<nav ng-transclude></nav>
<main ng-transclude></main>
<footer></footer>
</div>`
site.js
.directive("otSite", () => {
return {
scope: {},
transclude: true,
template: `
<div>
<header></header>
<nav></nav>
<main></main>
<footer></footer>
</div>`
site.js
<ot-site>
<div>
I’m transcluding the header.
</div>
<div>
I’m transcluding the menu.
</div>
<div>
I’m transcluding the body.
</div>
</ot-site>
index.html
<ot-site>
<div transclude-to="site-head">
I’m transcluding the header.
</div>
<div transclude-to="site-menu">
I’m transcluding the menu.
</div>
<div transclude-to="site-body">
I’m transcluding the body.
</div>
</ot-site>
index.html
.directive("otSite", () => {
return {
scope: {},
transclude: true,
template: `
<div>
<header></header>
<nav></nav>
<main></main>
<footer></footer>
</div>`
site.js
...
.directive("otSite", () => {
return {
scope: {},
transclude: true,
template: `
<div>
<header transclude-id="site-head"></header>
<nav transclude-id="site-menu"></nav>
<main transclude-id="site-body"></main>
<footer></footer>
</div>`
site.js
...
Custom transclusion
For element in clone:
1. Get target ID
2. Find target element with that ID
3. Append clone element to target
angular.forEach(clone, (cloneEl) => {
// get desired target ID
// find target element with that ID
// append element to target
});
custom-transclude.js
angular.forEach(clone, (cloneEl) => {
// get desired target ID
var tId = cloneEl.attributes["transclude-to"].value;
// find target element with that ID
// append element to target
});
custom-transclude.js
angular.forEach(clone, (cloneEl) => {
// get desired target ID
var tId = cloneEl.attributes["transclude-to"].value;
// find target element with that ID
var target = templ.find(`[transclude-id="${tId}"]`);
// append element to target
});
custom-transclude.js
angular.forEach(clone, (cloneEl) => {
// get desired target ID
var tId = cloneEl.attributes["transclude-to"].value;
// find target element with that ID
var target = templ.find(`[transclude-id="${tId}"]`);
// append element to target
target.append(cloneEl);
});
custom-transclude.js
angular.forEach(clone, (cloneEl) => {
...
target.append(cloneEl);
});
custom-transclude.js
angular.forEach(clone, (cloneEl) => {
...
if (target.length) {
target.append(cloneEl);
}
});
custom-transclude.js
angular.forEach(clone, (cloneEl) => {
...
if (target.length) {
target.append(cloneEl);
} else {
cloneEl.remove();
}
});
custom-transclude.js
angular.forEach(clone, (cloneEl) => {
...
if (target.length) {
target.append(cloneEl);
} else {
cloneEl.remove();
throw new Error(
`Target not found. Please specify the correct
transclude-to attribute.`
);
}
custom-transclude.js
...
Two parameters:
○ scope (optional)
○ callback function
□ passes clone
Transclude function
transclude (clone) =>
# DOM manipulation
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
custom-transclude.js
transclude((clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
custom-transclude.js
Transclude function access points
compile: (tElem, tAttrs, transclude) =>
link: (scope, iElem, iAttrs, ctrl, transclude) =>
controller: ($scope, $element, $transclude) =>
Transclude function access points
compile: (tElem, tAttrs, transclude) =>
link: (scope, iElem, iAttrs, ctrl, transclude) =>
controller: ($scope, $element, $transclude) =>
site.js.directive("otSite", () => {
return {
scope: {},
transclude: true,
template: `
<div>
<header transclude-id="site-head"></header>
<nav transclude-id="site-menu"></nav>
<main transclude-id="site-body"></main>
<footer></footer>
</div>`
...
site.js.directive("otSite", () => {
...
});
site.js.directive("otSite", () => {
...
compile: (tElem, tAttrs, transclude) => {
}
});
site.js.directive("otSite", () => {
...
compile: (tElem, tAttrs, transclude) => {
transclude((clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
site.js.directive("otSite", ($rootScope) => {
...
compile: (tElem, tAttrs, transclude) => {
transclude($rootScope.$new(), (clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
transclusion scope using $rootScope.$new()
proper transclusion scope
site.js.directive("otSite", ($rootScope) => {
...
compile: (tElem, tAttrs, transclude) => {
transclude($rootScope.$new(), (clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
site.js.directive("otSite", () => {
...
compile: (tElem, tAttrs, transclude) => {
transclude((clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
site.js.directive("otSite", () => {
...
compile: (tElem, tAttrs, transclude) => {
return (scope, iElem, iAttrs) => {
transclude((clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
...
site.js.directive("otSite", () => {
...
compile: (tElem, tAttrs, transclude) => {
return (scope, iElem, iAttrs) => {
transclude(scope.$parent.$new(), (clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
...
Code Demo
site.js.directive("otSite", () => {
...
compile: (tElem, tAttrs, transclude) => {
return (scope, iElem, iAttrs) => {
transclude(scope.$parent.$new(), (clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
...
Transclude function availability
compile: (tElem, tAttrs, transclude) =>
link: (scope, iElem, iAttrs, ctrl, transclude) =>
controller: ($scope, $element, $transclude) =>
site.js.directive("otSite", () => {
...
compile: (tElem, tAttrs, transclude) => {
return (scope, iElem, iAttrs) => {
transclude(scope.$parent.$new(), (clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
...
site.js.directive("otSite", () => {
...
(scope, iElem, iAttrs) => {
transclude(scope.$parent.$new(), (clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
site.js.directive("otSite", () => {
...
link: (scope, iElem, iAttrs, ctrl, transclude) => {
transclude(scope.$parent.$new(), (clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
site.js.directive("otSite", () => {
...
link: (scope, iElem, iAttrs, ctrl, transclude) => {
transclude(scope.$parent.$new(), (clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
site.js.directive("otSite", () => {
...
link: (scope, iElem, iAttrs, ctrl, transclude) => {
transclude((clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
Code Demo
Transclude function access points
compile: (tElem, tAttrs, transclude) =>
link: (scope, iElem, iAttrs, ctrl, transclude) =>
controller: ($scope, $element, $transclude) =>
Directive Life Cycle
Parent compile 1
2 Child compile
Parent controller 3
Parent pre-link 4
5 Child controller
6 Child pre-link
7 Child post-link
Parent post-link 8
Transclude function access points
compile: (tElem, tAttrs, transclude) =>
link: (scope, iElem, iAttrs, ctrl, transclude) =>
controller: ($scope, $element, $transclude) =>
Transclude function in ...
link / controller....
○ Auto-generates scope
○ No wrapper
○ Link: safest for DOM
manipulation
compile...
○ Scope issues
○ Wrapper code
○ Deprecated
.factory("MultiTransclude", () => {
return {
};
});
multi-transclude.js
.factory("MultiTransclude", () => {
return {
transclude: (elem, transcludeFn) => {
}
};
});
multi-transclude.js
multi-transclude.js.factory("MultiTransclude", () => {
return {
transclude: (elem, transcludeFn) => {
transcludeFn((clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
site.js.directive("otSite", () => {
...
link: (scope, iElem, iAttrs, ctrl, transclude) => {
transclude((clone) => {
angular.forEach(clone, (cloneEl) => {
var tId = ...
var target = ...
if (target.length) {...}
else {...}
});
});
...
site.js.directive("otSite", () => {
...
link: (scope, iElem, iAttrs, ctrl, transclude) => {
}
};
});
site.js.directive("otSite", (MultiTransclude) => {
...
link: (scope, iElem, iAttrs, ctrl, transclude) => {
}
};
});
site.js.directive("otSite", (MultiTransclude) => {
...
link: (scope, iElem, iAttrs, ctrl, transclude) => {
MultiTransclude.transclude(iElem, transclude);
}
};
});
Code Demo
Multi-transclusion model
Pros Cons
Flexible UI Open system
All in one directive Requires custom DOM manipulation
Extensible Less “HTML-like”
Re-usable for any component
Selectable List
Design
Selectable List
Design
Selectable List
Design
Behavioral PatternsBehavioral Patterns
<ot-list /> list.html
<ot-list
items="" />
list.html
<ot-list
items=""
selected="" />
list.html
<ot-list
items=""
selected=""
on-select="" />
list.html
<ot-list
items=""
selected=""
on-select=""
theme="" />
list.html
<ot-list
items=""
selected=""
on-select=""
theme="" />
list.html
<ot-list
items=""
selected="" />
list.html
Selectable List
Implementation
Areas Navigation
.controller("AppController", ($scope) => {
$scope.areas = {
list: [
"Floorplan",
"Combinations",
"Schedule",
"Publish"
],
current: "Floorplan"
};
});
app.js
<ot-site>
<div transclude-to="site-head">
I’m transcluding the header.
</div>
<div transclude-to="site-menu">
I’m transcluding the menu.
</div>
<div transclude-to="site-body">
I’m transcluding the body.
</div>
</ot-site>
index.html
<ot-site>
</ot-site>
index.html
<ot-site>
<ot-list></ot-list>
</ot-site>
index.html
<ot-site>
<ot-list
items="{{ areas.list }}"
selected="{{ areas.current }}"></ot-list>
</ot-site>
index.html
<ot-site>
<ot-list
items="{{ areas.list }}"
selected="{{ areas.current }}"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
.directive("otList", () => {
return {
template: ``
};
});
list.js
list.js.directive("otList", () => {
return {
template: `
<ul>
<li ng-repeat="item in items"
ng-bind="item"
ng-class="{'ot-selected': item === selected}"
ng-click="selectItem(item)">
</li>
</ul>`
};
...
list.js.directive("otList", () => {
return {
template: ...
};
});
list.js.directive("otList", () => {
return {
template: ...,
link: (scope, elem, attrs) => {
}
};
});
list.js.directive("otList", () => {
return {
template: ...,
link: (scope, elem, attrs) => {
scope.selectItem = (item) => {
scope.selected = item;
};
}
};
...
list.js.directive("otList", () => {
return {
template: ...,
link: (scope, elem, attrs) => {
scope.items = JSON.parse(attrs.items);
scope.selectItem = (item) => {
scope.selected = item;
};
}
};
...
list.js.directive("otList", () => {
return {
template: ...,
link: (scope, elem, attrs) => {
scope.items = JSON.parse(attrs.items);
scope.selected = attrs.selected;
scope.selectItem = (item) => {
scope.selected = item;
};
}
...
Code Demo
Shared scope is the
default behavior
Need to set the scope
property
No scope set...
When asked if shared scope was a good idea...
Apps Navigation
app.js.controller("AppController", ($scope) => {
$scope.areas = {
list: [
"Floorplan",
"Combinations",
"Schedule",
"Publish"
],
current: "Floorplan"
};
});
app.js.controller("AppController", ($scope) => {
$scope.areas = {...};
});
app.js.controller("AppController", ($scope) => {
$scope.areas = {...};
$scope.apps = {
list: [
"Marketing",
"Planning",
"Reservations",
"Settings"
],
current: "Marketing"
};
...
<ot-site>
<ot-list
items="{{ areas.list }}"
selected="{{ areas.current }}"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
<ot-site>
<ot-list
items="{{ areas.list }}"
selected="{{ areas.current }}"
transclude-to="site-body"></ot-list>
<ot-list
items="{{ apps.list }}"
selected="{{ apps.current }}"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
Code Demo
List component, no scope set
list.js.directive("otList", () => {
return {
template: ...,
link: (scope, elem, attrs) => {
scope.items = JSON.parse(attrs.items);
scope.selected = attrs.selected;
scope.selectItem = (item) => {
scope.selected = item;
};
}
...
list.js.directive("otList", () => {
return {
template: ...,
scope: true,
link: (scope, elem, attrs) => {
scope.items = JSON.parse(attrs.items);
scope.selected = attrs.selected;
scope.selectItem = (item) => {
scope.selected = item;
};
...
List component, scope: true
Code Demo
List component, scope: true (leak)
list.js.directive("otList", () => {
return {
template: ...,
scope: true,
link: (scope, elem, attrs) => {
scope.items = JSON.parse(attrs.items);
scope.selected = attrs.selected;
scope.selectItem = (item) => {
scope.selected = item;
};
...
list.js.directive("otList", () => {
return {
template: ...,
scope: {},
link: (scope, elem, attrs) => {
scope.items = JSON.parse(attrs.items);
scope.selected = attrs.selected;
scope.selectItem = (item) => {
scope.selected = item;
};
...
List component, scope: { }
list.js.directive("otList", () => {
return {
template: ...,
scope: {},
link: (scope, elem, attrs) => {
scope.items = JSON.parse(attrs.items);
scope.selected = attrs.selected;
scope.selectItem = (item) => {
scope.selected = item;
};
...
list.js.directive("otList", () => {
return {
template: ...,
scope: {},
link: (scope, elem, attrs) => {
scope.selectItem = (item) => {
scope.selected = item;
};
}
};
});
list.js.directive("otList", () => {
return {
template: ...,
scope: {
items: "=items",
selected: "=selected"
},
link: (scope, elem, attrs) => {
scope.selectItem = (item) => {
scope.selected = item;
};
...
list.js.directive("otList", () => {
return {
template: ...,
scope: {
items: "=",
selected: "="
},
link: (scope, elem, attrs) => {
scope.selectItem = (item) => {
scope.selected = item;
};
...
<ot-site>
<ot-list
items="{{ areas.list }}"
selected="{{ areas.current }}"
transclude-to="site-body"></ot-list>
<ot-list
items="{{ apps.list }}"
selected="{{ apps.current }}"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
<ot-site>
<ot-list
items="areas.list"
selected="areas.current"
transclude-to="site-body"></ot-list>
<ot-list
items="apps.list"
selected="apps.current"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
<ot-site>
<ot-list
items="areas.list"
selected="areas.current"
transclude-to="site-menu"></ot-list>
<ot-list
items="apps.list"
selected="apps.current"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
Code Demo
Attribute passing model
Pros Cons
Simple interface Not very extensible
Closed system Less flexibility for users
Dropdown
Design
<ot-dropdown>
<ot-trigger />
<ot-target />
</ot-dropdown>
dropdown.html
Dropdown
Implementation
Apps Navigation
<ot-site>
<ot-list items="areas.list"
selected="areas.current"
transclude-to="site-menu"></ot-list>
<ot-list items="apps.list"
selected="apps.current"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
<ot-site>
...
<ot-list items="apps.list"
selected="apps.current"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown>
</ot-dropdown>
<ot-list items="apps.list"
selected="apps.current"
transclude-to="site-body"></ot-list>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown>
<ot-list items="apps.list"
selected="apps.current"
transclude-to="site-body"></ot-list>
</ot-dropdown>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown>
<ot-list items="apps.list"
selected="apps.current"
transclude-to="site-body"></ot-list>
</ot-dropdown>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown>
<ot-list items="apps.list"
selected="apps.current"></ot-list>
</ot-dropdown>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown transclude-to="site-head">
<ot-list items="apps.list"
selected="apps.current"></ot-list>
</ot-dropdown>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown>
<span ng-bind="apps.current"></span>
<ot-list items="apps.list"
selected="apps.current"></ot-list>
</ot-dropdown>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown>
<span ng-bind="apps.current"
transclude-to="trigger"></span>
<ot-list items="apps.list"
selected="apps.current"
transclude-to="target"></ot-list>
</ot-dropdown>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown>
<span ng-bind="apps.current"
transclude-to="trigger"></span>
<ot-list items="apps.list"
selected="apps.current"
transclude-to="target"></ot-list>
</ot-dropdown>
</ot-site>
index.html
<ot-site>
...
<ot-dropdown>
<ot-trigger>
<span ng-bind="apps.current"></span>
</ot-trigger>
<ot-target>
<ot-list items="apps.list"
selected="apps.current"></ot-list>
</ot-target>
</ot-dropdown>
index.html
...
<ot-site>
...
<ot-dropdown>
<ot-trigger>
<span ng-bind="apps.current" />
</ot-trigger>
<ot-target>
<ot-list items="apps.list" ... />
</ot-target>
</ot-dropdown>
</ot-site>
index.html
Multi-transclude vs sub-directives
○ “HTML-like”
○ Built with separate parts
□ Maintain style of parts separately
□ Split options up into separate tags
dropdown.js.directive("otDropdown", () => {
return {
};
});
dropdown.js.directive("otDropdown", () => {
return {
scope: {}
};
});
dropdown.js.directive("otDropdown", () => {
return {
scope: {},
transclude: true
};
});
dropdown transclusion
index.html
trigger transclusion
target transclusion
<ot-site>
...
<ot-dropdown>
<ot-trigger>
<span ng-bind="apps.current" />
</ot-trigger>
<ot-target>
<ot-list items="apps.list" ... />
</ot-target>
</ot-dropdown>
</ot-site>
dropdown.js.directive("otDropdown", () => {
return {
scope: {},
transclude: true,
template: `
<div>
</div>`
};
});
dropdown.js.directive("otDropdown", () => {
return {
scope: {},
transclude: true,
template: `
<div ng-transclude>
</div>`
};
});
dropdown.js.directive("otDropdown", () => {
return {
scope: {},
transclude: true,
template: `
<div ng-transclude ng-click="toggleTarget()">
</div>`
};
});
dropdown.js.directive("otDropdown", () => {
return {
scope: {},
transclude: true,
template: `
<div ng-click="toggleTarget()" ng-transclude>
</div>`,
link: (scope) => {
scope.toggleTarget = () => {
scope.targetOpen = !scope.targetOpen;
};
...
.directive("otTarget",()=>{
return {
};
});
dropdown.js
.directive("otTrigger",()=>{
return {
};
});
.directive("otTarget",()=>{
return {
transclude: true
};
});
dropdown.js
.directive("otTrigger",()=>{
return {
transclude: true
};
});
.directive("otTarget",()=>{
return {
transclude: true,
template: `
<div>
</div>`
};
});
dropdown.js
.directive("otTrigger",()=>{
return {
transclude: true,
template: `
<div>
</div>`
};
});
.directive("otTarget",()=>{
return {
transclude: true,
template: `
<div ng-transclude>
</div>`
};
});
dropdown.js
.directive("otTrigger",()=>{
return {
transclude: true,
template: `
<div ng-transclude>
</div>`
};
});
.directive("otTarget", () => {
return {
transclude: true,
template: `
<div ng-transclude>
</div>`
};
});
dropdown.js
.directive("otTarget", () => {
return {
transclude: true,
template: `
<div ng-show="targetOpen" ng-transclude>
</div>`
};
});
dropdown.js
Code Demo
.directive("otTarget", () => {
return {
transclude: true,
template: `
<div ng-show="targetOpen" ng-transclude>
</div>`
};
});
dropdown.js
Dropdown - trigger/target share scope
Directive to directive communication
○ Controller as directive API
○ “Require” controller from other directives
□ link: (scope, elem, attrs, controller) =>
□ Caveat: must be on element or parents
○ Controller as directive API
○ “Require” controller from other directives
□ link: (scope, elem, attrs, controller) =>
□ Caveat: must be on element or parents
Directive to directive communication
dropdown.js.directive("otDropdown", () => {
...
template: `
<div ng-click="toggleTarget()" ng-transclude>
</div>`,
link: (scope) => {
scope.toggleTarget = () => {
scope.targetOpen = !scope.targetOpen;
};
}
});
dropdown.js.directive("otDropdown", () => {
...
template: `
<div ng-click="toggleTarget()" ng-transclude>
</div>`,
controller: function ($scope) {
scope.toggleTarget = () => {
scope.targetOpen = !scope.targetOpen;
};
}
});
dropdown.js.directive("otDropdown", () => {
...
template: `
<div ng-click="toggleTarget()" ng-transclude>
</div>`,
controller: function ($scope) {
$scope.toggleTarget = () => {
scope.targetOpen = !scope.targetOpen;
};
}
});
dropdown.js.directive("otDropdown", () => {
...
template: `
<div ng-click="toggleTarget()" ng-transclude>
</div>`,
controller: function ($scope) {
$scope.toggleTarget = () => {
scope.targetOpen = !scope.targetOpen;
};
}
});
dropdown.js.directive("otDropdown", () => {
...
template: `
<div ng-click="toggleTarget()" ng-transclude>
</div>`,
controller: function ($scope) {
$scope.toggleTarget = () => {
this.targetOpen = !this.targetOpen;
};
}
});
○ Controller as directive API
○ “Require” controller from other directives
□ link: (scope, elem, attrs, controller) =>
□ Caveat: must be on element or parents
Directive to directive communication
.directive("otTarget", () => {
return {
transclude: true,
template: `
<div ng-show="targetOpen" ng-transclude>
</div>`
};
});
dropdown.js
dropdown.js.directive("otTarget", () => {
return {
transclude: true,
require: "^otDropdown",
template: `
<div ng-show="targetOpen" ng-transclude>
</div>`
};
});
dropdown.js.directive("otTarget", () => {
return {
transclude: true,
require: "^otDropdown",
template: `
<div ng-show="targetOpen" ng-transclude>
</div>`,
link: (scope, elem, attrs, ctrl) => {
}
};
...
dropdown.js.directive("otTarget", () => {
return {
transclude: true,
require: "^otDropdown",
template: `
<div ng-show="targetOpen" ng-transclude>
</div>`,
link: (scope, elem, attrs, ctrl) => {
scope.ctrl = ctrl;
}
};
...
dropdown.js.directive("otTarget", () => {
return {
transclude: true,
require: "^otDropdown",
template: `
<div ng-show="ctrl.targetOpen" ng-transclude>
</div>`,
link: (scope, elem, attrs, ctrl) => {
scope.ctrl = ctrl;
}
};
...
Code Demo
Dropdown - trigger/target share scope
.directive("otTrigger",()=>{
return {
transclude: true,
template: `
<div ng-transclude>
</div>`
};
});
.directive("otTarget",()=>{
return {
transclude: true,
require: "^otDropdown",
template: `
<div
ng-show="ctrl.targetOpen"
ng-transclude>
</div>`,
link: ...
dropdown.js
.directive("otTrigger",()=>{
return {
transclude: true,
scope: {},
template: `
<div ng-transclude>
</div>`
};
});
.directive("otTarget",()=>{
return {
transclude: true,
scope: {},
require: "^otDropdown",
template: `
<div
ng-show="ctrl.targetOpen"
ng-transclude>
</div>`, ...
dropdown.js
Dropdown isolate scopes
Sub-directive model
Pros Cons
Flexible UI Open system
HTML-ish More boilerplate
Can modularize parts of the component Not extensible for new entry points
Application Scaffold
Conclusion
<ot-site>
<ot-dropdown transclude-to="site-head">
<ot-trigger />
<ot-target>
<ot-list items="apps" />
</ot-target>
</ot-dropdown>
<ot-dropdown transclude-to="site-head">
<ot-trigger />
<ot-target>
...
index.html
...
<div class="profile" />
<a href="/logoff" />
</ot-target>
</ot-dropdown>
<ot-list transclude-to="site-menu" items="areas" />
<div transclude-to="site-body">
<!-- app content -->
</div>
</ot-site>
index.html
<ot-app>
<!-- app content -->
</ot-app>
index.html
<ot-app></ot-app>
Barry Wong
Simon Attley
Caleb Morrell
Sara Rahimian
David Amusin
ALL OTHER PHOTOS CC0 / SPECIAL THANKS TO RYAN MCGUIRE
We’re hiring!
opentable.com/careers/
We’re hiring!
opentable.com/careers/
Questions?
Rachael L Moore
UI Engineer
morewry
Kara Erickson
Web Engineer
kara

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Reusable Components in Angular: Architecture, transclusion, and more