Introducing YUI

142
Introducing YUI Christian Heilmann Bucharest, Romania, November 2008

description

An introduction to YUI and some examples of how to use it to solve daily problems in web design. A talk given at the University in Bucharest and partly re-hashed on the flight from my Ajax Experience talk.

Transcript of Introducing YUI

Page 1: Introducing YUI

Introducing YUI

Christian Heilmann

Bucharest, Romania, November 2008

Page 2: Introducing YUI

Hello there, I am Chris.

Page 3: Introducing YUI

I am here today to talk about the Yahoo User Interface

library (YUI)http://developer.yahoo.com/yui/

Page 4: Introducing YUI

What is the most annoying thing about web

development?

Page 5: Introducing YUI

The undefined environment.

Page 6: Introducing YUI

This is also the coolest thing about web development.

Page 7: Introducing YUI

Don’t ever delude yourself into thinking you can control what people see when they

visit your web products.

Page 8: Introducing YUI

However, you want your products to work.

Page 9: Introducing YUI

The working bit is what I want to talk to you about.

Page 10: Introducing YUI

When you start writing a web document, you use HTML and

hope browsers do things right.

Page 11: Introducing YUI

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Here’s my title</title> <meta name="description" content="Description"></head><body></body></html>

Page 12: Introducing YUI

The right DOCTYPE, encoding and language is a great start.

Page 13: Introducing YUI

This is an unstyled document, right?

Page 14: Introducing YUI

Wrong.

Page 15: Introducing YUI

There is no such thing as an unstyled document.

Page 16: Introducing YUI

There is no such thing as an “unstyled page”.

Page 17: Introducing YUI
Page 18: Introducing YUI
Page 19: Introducing YUI
Page 20: Introducing YUI
Page 21: Introducing YUI

This differs from browser to browser and can be *very*

annoying!

Page 22: Introducing YUI

Which is why YUI offers you reset.css

http://developer.yahoo.com/yui/reset/

Page 23: Introducing YUI
Page 24: Introducing YUI
Page 25: Introducing YUI
Page 26: Introducing YUI
Page 27: Introducing YUI

Starting with a blank canvas is a good start.

Page 28: Introducing YUI

What about typography?

Page 29: Introducing YUI

Make it work across browsers with fonts.css

http://developer.yahoo.com/yui/fonts/

Page 30: Introducing YUI
Page 31: Introducing YUI

Even create layouts with grids.css

http://developer.yahoo.com/yui/grids/

Page 32: Introducing YUI
Page 33: Introducing YUI

All of this with one line of code pulling a document

from the web.

Page 34: Introducing YUI

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Here’s my title</title> <meta name="description" content="Description"> <link rel="stylesheet" href="http://yui.yahooapis.com/2.5.1/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css"></head><body></body></html>

Page 35: Introducing YUI

What if that file is not available?

Page 36: Introducing YUI

First of all, this is not a single server, but a world-wide

network of servers.

Page 37: Introducing YUI

Visitors of your site will get the files from the server geographically located

nearest to them.

Page 38: Introducing YUI

This is Yahoo’s network, but you can also use Google’s!

http://yuiblog.com/blog/2008/11/19/yui-google/

Page 40: Introducing YUI

If you like full control, then you can also host the files

yourself (this will also make them work off-line!)

Page 41: Introducing YUI

Let’s play with grids a bit.

Page 42: Introducing YUI

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Here’s my title</title> <link rel="stylesheet" href="http://yui.yahooapis.com/2.5.1/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css"></head><body>

<div id="doc" class="yui-t4"> <div id="hd">Header</div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> Main section </div> </div> <div class="yui-b"> Sidebar </div> </div> <div id="ft"><p>Footer</p></div></div></body></html>

Page 43: Introducing YUI

You divide the document in three parts: a head, a body

and a footer.

Page 44: Introducing YUI

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Here’s my title</title> <link rel="stylesheet" href="http://yui.yahooapis.com/2.5.1/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css"></head><body>

<div id="doc" class="yui-t4"> <div id="hd">Header</div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> Main section </div> </div> <div class="yui-b">

Sidebar </div> </div> <div id="ft"><p>Footer</p></div></div></body></html>

Page 45: Introducing YUI

You then define a side bar and a main part.

Page 46: Introducing YUI

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Here’s my title</title> <link rel="stylesheet" href="http://yui.yahooapis.com/2.5.1/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css"></head><body>

<div id="doc" class="yui-t4"> <div id="hd">Header</div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> Main section </div> </div> <div class="yui-b">

Sidebar </div> </div> <div id="ft"><p>Footer</p></div></div></body></html>

Page 47: Introducing YUI

Both are identical, the only difference is that the main

part has to be wrapped in a DIV with the ID “yui-main”.

Page 48: Introducing YUI

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Here’s my title</title> <link rel="stylesheet" href="http://yui.yahooapis.com/2.5.1/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css"></head><body>

<div id="doc" class="yui-t4"> <div id="hd">Header</div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> Main section </div> </div> <div class="yui-b">

Sidebar </div> </div> <div id="ft"><p>Footer</p></div></div></body></html>

Page 49: Introducing YUI

The order of the two doesn’t matter!

Page 50: Introducing YUI

The sidebar is a predefined width, the main section takes

up the rest of the space.

Page 51: Introducing YUI

You define the overall width of the document with an ID

and the location and width of the sidebar with a class on

the containing DIV.

Page 52: Introducing YUI

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Here’s my title</title> <link rel="stylesheet" href="http://yui.yahooapis.com/2.5.1/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css"></head><body>

<div id="doc" class="yui-t4"> <div id="hd">Header</div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> Main section </div> </div> <div class="yui-b">

Sidebar </div> </div> <div id="ft"><p>Footer</p></div></div></body></html>

Page 53: Introducing YUI

#doc = 750px

#doc2 = 950px

#doc3 = 100%

#doc4 = 974px

.yui-t1 = left 160px

.yui-t2 = left 180px

.yui-t3 = left 300px

.yui-t4 = right 180px

.yui-t5 = right 240px

.yui-t6 = right 300px

ID and width class, side and width

Page 54: Introducing YUI

You can add grid units inside the main section to divide it

into horizontal columns.

Page 55: Introducing YUI

Check the docs at:http://developer.yahoo.com/yui/grids

Page 56: Introducing YUI

Or if you are lazy...

Page 57: Introducing YUI

Use the grids builder:

Page 59: Introducing YUI

So where does this work?

Page 60: Introducing YUI

In all the browsers Yahoo has to support.

Page 61: Introducing YUI

Which are defined in our graded browser support:

http://developer.yahoo.com/yui/articles/gbs/

Page 62: Introducing YUI
Page 63: Introducing YUI

What if a new browser comes around?

Page 64: Introducing YUI

Chances are very high Yahoo will have to support it...

Page 65: Introducing YUI

...so check the YUI blog and update your YUI CSS include.

http://yuiblog.com

Page 66: Introducing YUI

Much faster than fixing everything yourself, right?

Page 67: Introducing YUI

What if HTML is not rich enough for your needs?

Page 68: Introducing YUI

Almost all *free* and *big* JavaScript libraries come

with widgets that work and are proven in the market.

Page 69: Introducing YUI

YUI is of course one of them.

Page 70: Introducing YUI

AutoComplete

Button

Calendar

Carousel beta

Charts [experimental]

Color Picker

Container

DataTable

ImageCropper beta

Layout Manager

Menu

Paginator

Rich Text Editor

Slider

TabView

TreeView

Uploader [experimental]

Page 72: Introducing YUI

Of course the other thing the YUI makes a lot easier is

using JavaScript.

Page 73: Introducing YUI

Both by fixing annoying browser bugs and by offering

convenience methods.

Page 74: Introducing YUI

For example:

Page 75: Introducing YUI

Wouldn’t it be cool to not know when to use which size

of the grid automatically?

Page 76: Introducing YUI

You can do that using YUI DOM.

http://developer.yahoo.com/yui/dom

Page 77: Introducing YUI

Using the DOM component, I can read out what happens in

the browser.

Page 78: Introducing YUI

I can get the dimensions of the window, the browser and of

any element in the document - regardless of its positioning.

Page 79: Introducing YUI

Thus I can create a YUI grid that works with all kind of different browsers sizes.

Page 81: Introducing YUI

YAHOO.example.autoGrid = function(){ var container = YAHOO.util.Dom.get('doc') || YAHOO.util.Dom.get('doc2') || YAHOO.util.Dom.get('doc4') || YAHOO.util.Dom.get('doc3') || YAHOO.util.Dom.get('doc-custom'); if(container){ var sidebar = null; var classes = container.className; if(classes.match(/yui-t[1-3]|yui-left/)){ var sidebar = 'left'; } if(classes.match(/yui-t[4-6]|yui-right/)){ var sidebar = 'right'; } function switchGrid(){ var currentWidth = YAHOO.util.Dom.getViewportWidth();

Page 82: Introducing YUI

if(currentWidth > 950){ container.id = 'doc2'; if(sidebar){ container.className = sidebar === 'left' ? 'yui-t3' : 'yui-t6'; } } if(currentWidth < 950){ container.id = 'doc'; if(sidebar){ container.className = sidebar === 'left' ? 'yui-t2' : 'yui-t5'; } } if(currentWidth < 760){ container.id = 'doc3'; if(sidebar){ container.className = sidebar === 'left' ? 'yui-t1' : 'yui-t4'; } }

Page 83: Introducing YUI

if(currentWidth < 600){ container.id = 'doc3'; container.className = ''; } }; switchGrid(); function throttle(method, scope) { clearTimeout(method._tId); method._tId= setTimeout(function(){ method.call(scope); }, 100); }; YAHOO.util.Event.on(window,'resize',function(){ throttle(YAHOO.example.autoGrid.switchGrid,window); });

}; return { switchGrid:switchGrid };}();

Page 84: Introducing YUI

What about monitoring the size of an element?

Page 85: Introducing YUI

position:fixed is sexy!

Page 86: Introducing YUI
Page 87: Introducing YUI

It can however also render your site impossible to use.

Page 88: Introducing YUI
Page 89: Introducing YUI

var YD = YAHOO.util.Dom; YAHOO.util.Event.onDOMReady(toggleMenu); YAHOO.util.Event.on(window,'resize',function(){ toggleMenu(); }); function toggleMenu(){ var sidebar = YD.getRegion('sb'); var browser = YD.getViewportHeight(); YD.setStyle('sb','position', browser < sidebar.bottom ? 'static' : 'fixed' ); }

Page 90: Introducing YUI

The DOM stepchild: Region

Page 91: Introducing YUI

Using Region I can find out the dimensions of an element.

Page 92: Introducing YUI

I can also find the region that is big enough to include two

regions, or the one that is the intersection of the two.

Page 93: Introducing YUI

region example

Page 94: Introducing YUI

YAHOO.util.Event.onDOMReady(function(){ var YD = YAHOO.util.Dom; var r1 = YD.getRegion('region-one'); var r2 = YD.getRegion('region-two'); var i = r1.intersect(r2); var u = r1.union(r2); var intersect = document.createElement('div'); document.body.appendChild(intersect); YD.setStyle(intersect,'position','absolute'); YD.setStyle(intersect,'background','#c0c'); YD.setStyle(intersect,'width',i.right-i.left + 'px'); YD.setStyle(intersect,'height',i.bottom-i.top + 'px'); YD.setStyle(intersect,'z-index',100); YD.setXY(intersect,i);

Page 95: Introducing YUI

var union = document.createElement('div'); document.body.appendChild(union); YD.setStyle(union,'position','absolute'); YD.setStyle(union,'background','#000'); YD.setStyle(union,'opacity',.5); YD.setStyle(union,'width',u.right-u.left + 'px'); YD.setStyle(union,'height',u.bottom-u.top + 'px'); YD.setStyle(union,'z-index',90); YD.setXY(union,u); });

Page 96: Introducing YUI

This gives me full control to avoid any overlap!

Page 97: Introducing YUI

What about things the browser does not tell me?

Page 98: Introducing YUI

Wouldn’t it be cool to find out when the font is resized?

Page 99: Introducing YUI
Page 101: Introducing YUI

You can detect the font size in several ways:

Page 102: Introducing YUI

Include an element with a known size in ems and read its

offsetHeight and offsetWidth in an interval...

Page 103: Introducing YUI

...or use an iframe with em sizing off-screen and subscribe

to its resize event.

Page 104: Introducing YUI

Or use the YUI container module anywhere on your

page... :)

Page 105: Introducing YUI

YAHOO.namespace('example.container'); YAHOO.util.Event.onDOMReady(function(){ YAHOO.example.container.module1 = new YAHOO.widget.Panel( 'module1', { close:true, draggable:true, constraintoviewport:true } ); YAHOO.example.container.module1.render(); YAHOO.widget.Module.textResizeEvent.subscribe( function(o){ console.log('Text has been resized!') } ); });

Page 106: Introducing YUI

YAHOO.namespace('example.container'); YAHOO.util.Event.onDOMReady(function(){ YAHOO.example.container.module1 = new YAHOO.widget.Panel( 'module1', { close:true, draggable:true, constraintoviewport:true } ); YAHOO.example.container.module1.render(); YAHOO.widget.Module.textResizeEvent.subscribe( function(o){ console.log('Text has been resized!') } ); });

Page 107: Introducing YUI

This works with one feature of YUI event that is very close to

my heart: Custom Events.

Page 108: Introducing YUI

Custom Events allow you to notify an unknown amount of

listeners about what is happening...

Page 109: Introducing YUI

... sending information not necessarily accessible to them

when it happens.

Page 110: Introducing YUI

Every single YUI component has a lot of Custom Events you

can subscribe to.

Page 111: Introducing YUI
Page 112: Introducing YUI

Say for example you want to make sure to securely chain

animation sequences...

Page 113: Introducing YUI

//This is the first animation; this one will //fire when the button is clicked. var move = new YAHOO.util.Anim("animator", { left: {from:0, to:75} }, 1); //This is the second animation; it will fire //when the first animation is complete. var changeColor = new YAHOO.util.ColorAnim( "animator", { backgroundColor: {from:"#003366", to:"#ff0000"} }, 1); //Here's the chaining glue: We subscribe to the //first animation's onComplete event, and in //our handler we animate the second animation: move.onComplete.subscribe(function() { changeColor.animate(); });

Page 114: Introducing YUI

//Here we set up our YUI Button and subcribe to //its click event. When clicked, it will //animate the first animation: var start = new YAHOO.widget.Button("startAnim"); start.subscribe("click", function() { //reset the color value to the start so that //the animation can be run multiple times: YAHOO.util.Dom.setStyle("animator", "backgroundColor", "#003366"); move.animate(); });

Page 115: Introducing YUI

//You can also make use of the onStart and onTween //custom events in Animation; here, we'll log all //of changeColor's custom events and peek at their //argument signatures: changeColor.onStart.subscribe(function() { YAHOO.log("changeColor animation is starting.", "info", "example"); }); changeColor.onTween.subscribe(function(s, o) { YAHOO.log("changeColor onTween firing with these arguments: " + YAHOO.lang.dump(o), "info", "example"); }); changeColor.onComplete.subscribe(function(s, o) { YAHOO.log("changeColor onComplete firing with these arguments: " + YAHOO.lang.dump(o), "info", "example"); });

Page 116: Introducing YUI

Knowing when something happens and being able to

react to it is much safer than assuming it worked.

Page 117: Introducing YUI

Knowledge is power.

Page 118: Introducing YUI

This is why YUI comes with a lot of tools to gain knowledge

about what is happening under the hood of your

application.

Page 119: Introducing YUI

YUI Logger gives you a cross-browser console to show

values.

Page 120: Introducing YUI
Page 121: Introducing YUI

Death to alert()!

Page 122: Introducing YUI

All YUI components come as a debug version which log

everything that is going on to the logger.

Page 123: Introducing YUI

You can even include the logger on the fly with a

bookmarklet.

Page 125: Introducing YUI

If you need even more control, there is the YUI

profiler.http://developer.yahoo.com/yui/profiler/

Page 126: Introducing YUI
Page 127: Introducing YUI

And the YUI test framework for test driven development.

http://developer.yahoo.com/yui/yuitest/

Page 128: Introducing YUI

On a code level, YUI comes out-of-the-box with

namespacing.

Page 129: Introducing YUI

Which – if used correctly – keeps large amounts of code readable and maintainable.

Page 130: Introducing YUI

YAHOO.lang also comes with a lot of validation methods to ensure things are what they

are.

Page 131: Introducing YUI

So how is YUI good for professional and easy

development?

Page 132: Introducing YUI

Built on agreed standards

Separated into modules each dealing with one task

Constant reporting of what is going on

Own Debugging environment

Page 133: Introducing YUI

Here’s another small thing I prepared earlier:

Page 134: Introducing YUI
Page 135: Introducing YUI

Using Event and Dom I can control the visible part:

Page 136: Introducing YUI

function move(e){ y = YAHOO.util.Event.getXY(e); if(y[1] > size){ render(y); } }; function render(y){ var d = YAHOO.util.Dom; var real = y[1] - d.getDocumentScrollTop(); d.setStyle(top,'height',real-size+'px'); d.setStyle(bottom,'top',real+size+'px'); var h = d.getViewportHeight() - real + size; d.setStyle(bottom,'height',h + 'px'); };

Page 138: Introducing YUI

What does the future hold?

Page 139: Introducing YUI

YUI3http://developer.yahoo.com/yui/3/

Page 140: Introducing YUI
Page 141: Introducing YUI

Include on demand

Multiple sandboxed instances in a page

Modularity on CSS level (per element reset)

Every event is a custom event

Page 142: Introducing YUI

Christian Heilmann

http://wait-till-i.com | http://scriptingenabled.org

http://twitter.com/codepo8

THANKS!