Introduction to Office UI Fabric

Posted by: Mark Rackley on October 05, 2015

Shocker, I’m not a design expert. Don’t get me wrong, I can do design work. I can create a user experience that is simple, elegant, and gets the job done. It’s just going to take me a lot longer than someone who does design for a living, and I’m sure it can be improved on by someone who focuses solely on design. I know.. I know.. you thought I was perfect. Sorry to all the fans I’ve let down.

I mean, the countless hours I’ve spent over my lifetime looking for icons to use, tweaking colors to make sure the are “web friendly”, styling tables and panels so that they have SOME sort of style to them.  Sometimes the look and feel of an application takes longer to create than the actual functionality behind the scenes to DO the work.

You might be a developer if…

So, I was immediately interested when I heard about Office UI Fabric.  What is this Office UI Fabric you may be asking? Excellent question! Well, according to the site:

The Office UI Fabric is The front-end framework for building experiences for Office and Office 365. It is a responsive, mobile-first, front-end framework, designed to make it quick and simple for you to create web experiences using the Office Design Language. It's easy to get up and running with Fabric - whether you're creating a new Office experience from scratch or adding new features to an existing one.

Okay… so what is it??

Office UI Fabric is a couple of css files that help you implement the design aspects of your web applications. It has features such as Panels, Dialogs, Callouts, Spinners, Date Pickers… etc.. etc…  In addition is has a set of Typography features for font sizes and colors which help you stay consistent in your development of applications. It also has a TON of useful icons to choose from (this is by far one of my favorite features).  These icons can be made larger and smaller and have the color affected by the Typography features. That just saved future me hours of hunting for the one icon I need (they even have a cat icon… really???)  No.. they don’t have a bacon icon.. :(

Anyway, another thing that’s really great is that you can start using Office UI Fabric IMMEDIATLEY. It completely compliments any client side development efforts you are currently undertaking. In fact, since it’s just css files you don’t even have to be on Office 365 or SharePoint 2013.

In an effort to learn Office UI Fabric I put together a session for the COLLAB365 Online Conference about it. My session is at 10:00am Central time on Thursday October 8th if you want to check it out. In this session I’ll walk you through the basics of getting started with Office UI Fabric. We’ll build up some functionality using some of the key features I’ve played with in Office UI Fabric:

  • Typography
  • Icons
  • Tables
  • Responsive Grid
  • Callouts

Of course, we’ll just be scratching the surface but it will give you the basics you need to get off and running. The final demo of the session pulls together all of the above features into one example. That script is also the script used in this blog post to highlight these same features.

Get on with it! How do we use Office UI Fabric

Okay.. okay.. sheesh.. impatient much? There are a couple of things you need to be aware of when using Office UI Fabric (as of the writing of this blog).

It’s not 100% baked

This is new functionality. Not everything works 100% yet. You’ll find some quirks trying out different features and components. For instance, there is an “Overlay” component that can be used to gray out a portion of the screen, however the functionality is not implemented to prevent interaction with the components underneath the overlay. You’ll need to write some of this functionality yourself.  In fact you can submit your changes to GitHub if you want to be really helpful and they may integrate them into the framework: https://github.com/ngOfficeUIFabric/

You’ll need to write some JavaScript

For some of the components you will need to write some (a lot?) of JavaScript for them to actually work. A great example of this is the callouts. If you just implement a callout as the web site shows you, you will indeed see a callout on the screen.  However, the callout will always be visible. It likely won’t be positioned on the page where you want it and it won’t disappear when you click the “x” icon.  You have to write all of that functionality yourself.  My hope is that someday in the future there will be a corresponding JS file to go along with the Office UI Fabric css files that does most of this heavy lifting for you (think jQuery UI).  In my example script below we’ll be implementing the callouts and the JavaScript necessary to make it work.

The Example

Okay, now that we have the caveats out of the way. Let’s dig into the example.  In the example script below we are going to implement a few of the features of Office UI Fabric:

  • Responsive Grid – We are using SharePoint’s REST to query an Issues list and populate the results of the query into the Responsive Grid Component.
  • Icons – We are creating an “information” icon as the first column and using the Icon feature to display the icon.
  • Typography – We are using the Typography feature to set the font size and color of the header row of the Grid as well as to set the size of the color of the Icon.
  • Callouts – When a user clicks on the icon, we’ll be using the callouts component to pop-up a callout that displays content.  Yes, there is JavaScript to show the callout, hide the callout, and position the callout accordingly.
  • Tables – Within the callout we are displaying information about the issue selected after clicking on the corresponding icon. The information in this callout is retrieved using REST and formatted using the Table component of Office UI Fabric.

In order to use the script below “as is”, you’ll need an Issue Tracking list named “Issues” in Office 365 (because of the REST queries).  You could easily tweak the script below to work with other versions of SharePoint by changing the REST queries and data manipulation accordingly.  As usual, I’ve also thrown together a quick video walking you through using the script.

If this is just too much content to throw at you at once, please check out my session on Thursday and I’ll walk through in a lot more detail.

The Script

Here’s the script. Remember, this is a teaching aid, not a “throw into production” solution.

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>

<!-- References to Office UI Fabric -->
<link rel="stylesheet" href="//appsforoffice.microsoft.com/fabric/1.0/fabric.min.css">
<link rel="stylesheet" href="//appsforoffice.microsoft.com/fabric/1.0/fabric.components.min.css">

<!-- Callout div -->
<div class="ms-Callout ms-Callout--arrowLeft" id="callout" style="display:none">
<div class="ms-Callout-main">
<div class="ms-Callout-header">
<p class="ms-Callout-title">Callout Left Arrow <i class="ms-Icon ms-Icon--x" onclick="$('#callout').fadeOut();"></i></p>
</div>
<div class="ms-Callout-inner">
<div class="ms-Callout-content">
<p class="ms-Callout-subText ms-Callout-subText--s" id="calloutContent"></p>
</div>
</div>
</div>
</div>

<!-- Responsive Grid -->
<div id='issueGrid' class="ms-Grid">
<div class="ms-Grid-row ms-font-l ms-fontColor-blue">
<span class="ms-Grid-col ms-u-sm1"></span>
<span class="ms-Grid-col ms-u-sm3">Title</span>
<span class="ms-Grid-col ms-u-sm2">Assigned To</span>
<span class="ms-Grid-col ms-u-sm2">Priority</span>
<span class="ms-Grid-col ms-u-sm2">Status</span>
<span class="ms-Grid-col ms-u-sm2">Due Date</span>
</div>
</div>

<script type="text/javascript">

var call = $.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Issues')/items?$select=Id,Title,AssignedTo/Title,Status,Priority,DueDate&$expand=AssignedTo",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=nometadata"
}

});
call.done(function (data,textStatus, jqXHR){
//add issus to issueGrid using results of REST query
rows = '';
for (index in data.value)
{
rows += '<div class="ms-Grid-row">';
rows += '<span class="ms-Grid-col ms-u-sm1"><i class="ms-font-xl ms-fontColor-alert ms-Icon ms-Icon--infoCircle" onclick="GetInfo(this,'+data.value[index].ID+')"/></span>';
rows += '<span class="ms-Grid-col ms-u-sm3">'+data.value[index].Title+'</span>';
rows += '<span class="ms-Grid-col ms-u-sm2">'+data.value[index].AssignedTo.Title+'</span>';
rows += '<span class="ms-Grid-col ms-u-sm2">'+data.value[index].Status+'</span>';
rows += '<span class="ms-Grid-col ms-u-sm2">'+data.value[index].Priority+'</span>';
rows += '<span class="ms-Grid-col ms-u-sm2">'+data.value[index].DueDate+'</span>';

rows += '</div>';
}
$("#issueGrid").append(rows);

});

call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving Issues: " + jqXHR.responseText);
});

function GetInfo(element,id)
{
//get information for specific issue (Id of clicked on Isse in grid)
var call = $.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Issues')/items?$select=Id,Title,AssignedTo/Title,Status,Priority,DueDate&$expand=AssignedTo&$filter=(ID eq "+id+")",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=nometadata"
}

});
call.done(function (data,textStatus, jqXHR){

$("#calloutContent").empty();
//build Table with results of REST Query
var content = '<div class="ms-Table">';
content += '<div class="ms-Table-row"><span class="ms-Table-cell">TITLE:</span><span class="ms-Table-cell">'+data.value[0].Title+"</span></div>";
content += '<div class="ms-Table-row"><span class="ms-Table-cell">ASSIGNED TO:</span><span class="ms-Table-cell">'+data.value[0].AssignedTo.Title+"</span></div>";
content += '<div class="ms-Table-row"><span class="ms-Table-cell">STATUS:</span><span class="ms-Table-cell">'+data.value[0].Status+"</span></div>";
content += '<div class="ms-Table-row"><span class="ms-Table-cell">PRIORITY:</span><span class="ms-Table-cell">'+data.value[0].Priority+"</span></div>";
content += '<div class="ms-Table-row"><span class="ms-Table-cell">DUE DATE:</span><span class="ms-Table-cell">'+data.value[0].DueDate+"</span></div>";
content += '</div>'

$("#calloutContent").append(content);

//place callout after icon to positioning
$(element).after($("#callout"));

//get position of the icon clicked on
var pos = $(element).position();

//position the callout correctly over the icon
//tweaking numbers to get it correct
$("#callout").css({
position: "absolute",
top: pos.top - 55,
left: (pos.left) + 10
});

//show the callout
$("#callout").fadeIn();

});

call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving Issues: " + jqXHR.responseText);
});
}

</script>

 

The Video

The Conclusion

As you can see, it’s pretty darn easy to get started with Office UI Fabric. I intend to keep using it and look forward to it being improved upon / added to. Whether you are writing SharePoint Hosted Add-Ins, Provider Hosted Add-Ins, or just throwing JavaScript on a page you can take advantage of Office UI Fabric.. heck you don’t even have to be creating an Office or SharePoint application and still take get the benefits. So, what’s stopping you? Go.. play.. get your hands dirty… and enjoy!

Topics: Microsoft Office 365

    Recent Posts

    Categories