bootstrap

All posts tagged bootstrap

menu-title

Designing our new internal marketing tools has been a pet project of mine for a couple of weeks now.  I spend a little time here and there adding information and tools for our employees to better understand our relationship with our clients.  I’m displaying basic contact information, some information on the kind of business we do with them, a log of notes we take, past business, and company information.  All of that equates to a rather long page of information.  All of the information is relevant and displayed in an attractive compact manner the problem is it is just so much information that it can be overwhelming.  I showed it to a coworker and he wanted tabs to flip between the various bits of information because he found it overwhelming.  I didn’t want to go to tabs because of the increased difficulty that would be added when they wanted to print off the marketing information and I didn’t feel that tabs were the right way to go here.  With that in mind I needed another solution for a large page of information that could be easily navigable…enter Bootstrap ScrollSpy.

Anyone who has looked at the Bootstrap documentation will immediately notice that has exactly the same problem I’m having; long page with lots of information.  On the left they have a static menu that follows you and highlights what section you are currently in.  Clicking on any one of those links will bring you to that portion of the page.  If it’s good enough for them it’s good enough for me so I set about recreating their side menu style.

ScrollSpy can be used with any .nav menu whether it is one your create with a ul or if it is the main nav-bar at the top of the screen.  ScrollSpy works by checking what element is currently at the top of the visible screen and adding making that nav element active.  This is great but there are a few quirks and tweeks that took me a while to figure out.

At the most basic level you want to add the HTML 5 data-spy attribute to your body because that is what we are going to spy on.  Here is the example code from Bootstrap’s documentation.

The data-target attribute needs to point to the parent element of your nav.  So if you have a nav-bar you’ll need to have an outer div or element with the id that you will use in your ScrollSpy.

Now all of your elements in your nav will need to have an href pointing to the id of the element you will be spying on.  These will need to be valid elements in your DOM.  If you are adding things dynamically and need to update your ScrollSpy use

That’s it for the simple example.  Set the data-spy and data-target on your body tag and you should be all set assuming your nav is set up properly.  One issue you may run into is not spying on the body.  When I originally started this I wasn’t spying on the body tag because I was loading in a template using Django’s block tags.  Granted it was within the body tag at one point but not specifically and I wanted to only watch my div.  ScrollSpy will only work correctly on non-body tag elements if they have a fixed height and a scroll bar.  If you do not have a scroll bar on the element you are spying on it will not work.  Your nav-bar will always have the last element as active.  To fix this just give the element you are spying on a fixed height and you should be good.  In my case instead of using the HTML 5 tags on the body I used JavaScript.  The body tag is used in every page and I didn’t want the ScrollSpy loaded on every single page.  In my JavaScript for my marketing page I have the line $("body").scrollspy({target: "#side-nav", offset:50});.  The offset tells ScrollSpy by how many pixels to offset the scroll before we are actually “in” the element.  I added some padding so when you see the header near the top we are now in the section.

To mimic the fixed sidebar used on the Bootstrap documentation use either the Affix JavaScript from Bootstrap or the affix class on your nav.  Below I have my full example and how it all works.

HTML

 JavaScript

I hide the side nav on smaller resolutions to give more screen real estate. The a tags have both a name and an id because the id is used by ScrollSpy and the name is used as a normal locator on a page.  Let me know what you think and if there are any questions or comments.

As Bootstrap nears releasing their final version of Bootstrap 3 they will continue to roll out Release Candidates for bug fixing and testing.  We have elected to keep as up to date with them as we can under the assumption that release candidates are fairly stable and shouldn’t change too much between now and release.  Tuesday they released their next release candidate, RC2.  You can read about the full changes at their blog but I’m going to highlight a few of the main things that changed for us.

Default Col is Gone

In RC1 we were using col- to be the default size for all devices.  RC2 has removed this default class.  Instead they have added a new -xs size for really small phones and a -md, though I think this was in RC1 we just never used it.  The best way to do a default size for most devices is now to use to col-sm-.  Use the small version because it works for devices with a screen resolution  of ≥768px.  This is what most modern devices are using.  Anything lower than that will default your spans to be like a col-12.  If you need to make changes for screens smaller than that use col-xs-.

Navbar Changes

Some little things changed with the navbar.  Just quick changes to classes.  You’ll need to wrap the mobile navbar header in a div with the class navbar-header.  Change nav-collapse to navbar-collapse.

Large and Small or lg and sm?

To keep consistency throughout Bootstrap they have changed many of the button classes from button-large to button-lg and button-small to button-sm. Form groups is also affected by this change.

Those are some of the main things we found.  I’m sure there are plenty of things that are affecting other people.  Head over to their blog to read all about it.  I’ll update this post if I come across anything else.

EDIT Aug 16 12:06PM

It seems we’ve stumbled upon a bug in their Modal.  When a modal is opened and then closed when you reopen it you can’t close it with the x or the close button. They are aware of the issue.  Our modals are actually all out of whack and we are looking into that right now which is how we found that bug.

EDIT Aug 16 12:20PM

Modals loaded remotely are being loaded into the modal and not the modal-body.  To fix this just move all of the divs below the modal into your remote document and that should work for now.  It’s an issue they are discussing.

EDIT Aug 19 1:01PM

When you print it uses the col-xs tag.  Make sure to update accordingly.

typeaheadjs

 

New Version – Twitter Typehead.js v0.10

Bootstrap dropped support for their bootstrap-typeahead.js and suggested we all use Twitter’s Typeahead.js.  This was a pretty big change for us and we encountered a bit of a learning curve because the documentation wasn’t exactly what we had hoped it would be.  Typeahead.js is incredible and can do some amazing things though and we are happy to be using it.

The first issue is that right out of the box it will not work with Bootstrap 3.  Support is still being looked at but in the mean time there are some fixes.  Here is a thread about the issue on their GitHub page with some helpful people posting solutions for the interim.  Below is the code we are using for ours which I’m sure will change in the coming days.

Once we got the formatting and could create our typeahead inputs we needed data for them.  We were using AJAX calls to our server to get the list as the user typed.  This worked but was a little slow.  Typeahead now has a few different ways of populating data into the input field.

Local

Local is a list of hard coded values right in your JavaScript

Prefetch

When the page is loaded the ajax call is made and stored in the browser’s local storage so the next time you visit the page it does not need to make another request unless the time-to-live has expired.  The ttl is defaulted to one day but you can change that in the options you pass.  One thing to note is that your browser has a limited amount of storage for this type of thing.  When we tried to pull our contacts list all in one go it would crash typeahead because the browser ran out of memory for local storage.  Our fix for this was that the prefetch URL would only return a limited number of entries, for example, the last 1000 updated contacts.

Remote

If the user starts to try and type something that isn’t immediately available through prefetch or local it falls back to your remote URL.  Just place the %QUERY tag in your URL and it will take what the user has entered and make a call to your server for a list using that query.

Full Example:

Templates

Typeahead supports your own custom templates for the drop down.  In our case we are just making the customer number bold and displaying their name but you could do everything from changing fonts, making inline lists, or even displaying images inline with your results.  Very cool.

Datums

We learned the hard way about a few different things when formatting results for your typeahead.  Typeahead expects a list of dictionaries with certain values, these are called datums.  Only two keys are required by typeahead, value and tokens.  After that you can add your own keys to be used by the typeahead for whatever purpose you’d like, whether it’s changing what happens on click or an image to be placed inline.  The value is the actual value of the typeahead, what the user is trying to get to.  Tokens are a list of strings that help the user get to where they want to go.  One very important note, the tokens must be single word strings — no spaces.   Spaces come across as %20 and when the user enters a normal space the typeahead doesn’t recognize that correctly.  So what we did was split the name of the company/contact into a list of separate strings for each word and that worked great.

Onward

Still playing around with it but I feel like we have a pretty good grasp on it.  Here is a list of their examples just to help drive the point home a bit further. Just waiting on the Bootstrap 3 support but overall we are happy with the new typeahead after we spent a long time trying to figure out all of the quirks we were having with it.

Hope this is helpful to others out there.

EDIT 16 September 2013

Some people were complaining about the dropdown not dynamically scaling.  Git user karimlahlou has posted this to fix that problem.

Thank you @ashleydw, finally i got it working. for those need to adjust the list width, use this function:

Hope this helps a few people.

Bootstrap 3 Change Notes

blurred code

After spending the last two days working with Bootstrap 3 I wanted to jot down a few of the things I found in case I forget them tomorrow or to help anyone else running into similar issues.

Forms, Grid System, and Screen Resolutions

We have converted our forms from form-horizontal to the default Bootstrap form.  We think it looks better with the new update.

Old code:

New code:

Cleaned up a bit as we had that extra label tag on our old code which was unnecessary as Django adds the label tag correctly on render.  One of the biggest thing to notice is that the span4 tag changed to col-4.  Bootstrap is now responsive by design and mobile first which means that the col- tags are meant for display on a mobile device but are the default when no other tags are found.  This gives us the ability to make pages and forms that appear differently depending on screen resolution.  There are three tags for column spacing, col- for mobile devices, col-sm- for tablet screens, and col-lg- for desktops.  To use the various screen sizes just add on more tags.  The sizes will appear when viewed on different resolutions.  Making the above example take up the whole row on a mobile device and only a third of the screen on a desktop would change the code like so:

Easy enough right?  I thought so too!  Not all of us will have a mobile device or tablet ready to go to test our site on different resolutions.  You can always just resize your window to see what happens but that is annoying too.  Luckily, we found a great site that will take any URL and display the page at any screen resolution and has a lot of built-in resolutions for common devices called Screenfly.  This even works for pages hosted locally.

Modals

Modals had a lot of changes made to them which made us redo all of our current modals because they were all broken.  Example:

Old code:

New code:

A few things to note, the first of which is that the old code was using JavaScript to load in the contents of a page into the modal. Turns our Bootstrap did this already if you just provided the link in the href.  Now we know.  The hide class has been removed.  When the modal is opened Bootstrap adds the modal-open class to the body which stops scrolling on the page and limits it to the modal only.  Pretty awesome.  Make sure your JavaScript files are being loaded at the bottom of your HTML pages to ensure this works correctly.

One thing we have been trying to do is put all Save buttons on the right side of our pages and modals.  Using the Bootstrap pull-right class floats objects to the right.  The problem with this is the modal-content div will not grow with floated objects.  More specifically no outer div will grow with floated elements by default.  To get around this you simply need to add a bit of CSS.

This works perfectly for any modal with a floated right button at the bottom but if there isn’t anything floated at the very bottom of the modal this will add an extra 15px to the padding, which we don’t want. To fix that just make that CSS a bit more specific by using id’s on the modals you want to specifically fix.  I have made a jsFiddle which demonstrates exactly what I am talking about.

Closing

That’s it so far.  Most of that took a really long time to figure out.  Bootstrap dropped their typeahead JavaScript in favor of typeahead.js which I’m still trying to figure out fully.  I’ve got it to work but there are some really weird things happening that I think have to do with Bootstrap 3.  The biggest problem we are running into is that Bootstrap 3 is so new no one has written much about how to fix little problems like the ones we’ve had so far and other utilities haven’t updated to fully support it yet so we soldier on.

Let me know if you have any other tips or if you’ve had anything that has tripped you up so far.

EDIT – 8/1/13

Bootstrap dropped support for their own bootstrap-typeahead.js and has instead suggested typeahead.js by Twitter.  One of the biggest problems with that right now is typeahead.js does not support Bootstrap 3 and so the CSS is messed up.  Here is an example I wrote up quickly to demonstrate it and I have commented on their GitHub project page on the issue of Bootstrap 3 CSS problems..  They say they are aware of it and the CSS is on their TODO list.  Here’s to hoping it happens quickly!

Bootstrap 3 RC1

Bootstrap 3

Creating our new site required a better base.  We chose to use the popular Bootstrap CSS engine to unify our CSS and help us get things started quickly.  After using Bootstrap 2.3.2 for nearly three months Bootstrap 3 was released.  We had hoped that the update would not affect our code base but we were wrong.  Nearly everything has changed in our styling.  Bootstrap 3 has gone for a mobile-first approach making even the basic framework responsive.  This is great for most developers, and even for us, because we can make wonderful sites that look and feel great on all platforms.  Our company had talked about wanting mobile versions of applications but we had put that on the back burner as many of our applications involve data entry.  Converting our project to use Bootstrap 3 has been a headache due to changes to simple classes that were used before but we are moving forward and should have things squared away by tomorrow.  The site looks cleaner, runs faster, and works on mobile already.  All in all it has been for the best but the headache will continue probably through the rest of the week as we smash all the bugs related to their new JavaScript and CSS.

Let me know if you’re using it and what you think about the changes.