• Stars
    star
    647
  • Rank 69,579 (Top 2 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 11 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

enhance your HTML5 progress bars with minimal effort!

Build Status

alt tag

enhance your HTML5 <progress> bars with minimal effort!

alt tag

The <progress> element doesn't always play nice.

It doesn't have a consistent appearance across the popular browsers. In addition, different browsers impose different limitations on the <progress> element.

Because of this, it's often overlooked in favor of styled <div> combos.

ep tackles this;

  • Cross browser reset and styling to pull <progress> element in line with modern slim-style bars
  • CSS helpers that provide classes and attributes to deal with things like positioning, growth style, simulation etc.
  • Optional JS helper for better control and interaction with <progress> elements. For example; being able to hook into network request status and display this to the end user.
  • Plays nice wherever the <progress> element is supported!
  const myEp = new Ep(document.querySelector('progress'));
  const makeRequest = () => {
    myEp.simulate();
    const xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        myEp.complete();
      }
    };
    xhttp.open('GET', '/index.html', true);
    xhttp.send();
  };
  (myEp._VALUE) ? myEp.set(0, makeRequest) : makeRequest();

Index

Browser support

Chrome Firefox Safari Opera Edge IE(10+)
😄 😄 😄 😄 😄 😄

Caveats

  • iOS Safari doesn't like indeterminate <progress> elements. Get round this by not setting your <progress> element to be indeterminate but instead using the helper class ep--indeterminate which will mock the indeterminate state.
  • In IE, ensure that the max attribute is set when using specific values. If no max is set, the value won't go higher than 1 😢

Usage

You have various options with how to use ep;

  • Use the stylesheet by adding ep.css to your app.
  • Include the optional JS helper ep.js for some more control.
  • Integrate the ep styles with your own SASS by importing the parts you need.

Install

You can grab a copy of ep through bower or npm;

  bower install ep

  npm install @jh3y/ep

Just using the stylesheet

If you're just using the stylesheet, you just need to include it. No alterations need to be made to your current <progress> elements unless you want to make use of some of the helper classes and attributes.

  <progress value="75" max="100"></progress>

  <progress class="ep--fixed ep--top" value="75" max="100"></progress>

  <progress data-simulate="true" value="75" max="100"></progress>

Need to change the color of a <progress> element or something else? Override the rule. For example, change the color to purple;

  progress {
    background: purple;
  }

  progress::-moz-progress-bar {
    background: purple;
  }

  progress::-webkit-progress-bar {
    background: purple;
  }

  progress::-webkit-progress-value {
    background: purple;
  }

There is also a SASS mixin included to as a shorthand to do this;

@import '~ep/mixins';
.progress {
  @include color-bar(purple);
}

Including the optional JS helper

If you choose to use the optional JS helper. You'll have access to the Ep constructor class. Refer to the JS API for further info.

  const el   = document.querySleect
  const myEp = new Ep()

Integrating with your own SASS files

Say you want to pick and choose pieces of ep. Simple. This is actually the easiest way to override eps configuration variables. ep makes use of the !default flag in sass to make this possible. For example; let's say we only want the core styles but we don't want any opacity and we want the primary color to be be purple.

  $ep-fg: purple;
  $ep-opacity: 1;
  @import '~ep/core'

CSS Helpers API

Without the use of JS, ep provides some CSS helpers in the form of attributes and classes you can apply to <progress> elements to define behaviors.

Aesthetic helpers

Aesthetic helpers come in the form of classes;

  • ep--top - position absolute top
  • ep--bottom - position absolute bottom
  • ep--fixed - position fixed
  • ep--spread - change style of <progress> bar to spread
  • ep--indeterminate - show indeterminate state

Behavioural helpers

Behavioural helpers come in the form of attributes that must be applied to your <progress> elements;

  • data-complete - complete progress(set to 100 and hide, more control with JS helper)
  • data-simulate - slowly simulate progress in steps up to 99%over 30 seconds(request timeout), can be configured/overrode
  • data-mock="value" - mock progress for given number of seconds
  • data-staggered-mock="value" - mock progress but with staggered style
  • data-timer="value" - use progress element as timer for given seconds
  • data-pause - pauses any animation in place such as timer, mock etc.

NOTE:: The mock, staggered-mock, timer and simulate behaviors have duration defaults set in the source. For example; the max duration is set at 4. This is to keep the size down. But these can easily be altered by building your own version of ep or adding rules for the durations you require. For example; I want the simulation to only take 10s and a timer that will take 15s.

progress[data-simulate] {
  animation-duration: 10s;
  animation-timing-function: steps(28);
}

progress[data-timer="15"] {
  animation-duration: 15s;
}

Sass variables

ep leverages the !default flag in Sass so it's easier to override ep configuration variables. Simply set any of the following variables before importing ep.

  • $ep-ns: ep - set the class helper prefix
  • $ep-height: 6px - set the height of <progress> elements
  • $ep-fg: #3498db - set the primary color of <progress> elements
  • $ep-indeterminate-fg: $ep-fg - set the primary color when in indeterminate state
  • $ep-opacity: .6 - set the opacity of <progress> elements
  • $ep-transition: .25s - set the duration for <progress> elements to transition value
  • $ep-timeout-threshold: 30 - the time it takes for simulation to complete in seconds
  • $ep-simulate-max: 99 - at which value should simulation stop
  • $ep-mocks-min: 1 - minimum mocking duration in seconds
  • $ep-mocks-max: 4 - maximum mocking duration in seconds
  • $ep-staggered-mocks-min: 1 - minimum staggered mock duration in seconds
  • $ep-staggered-mocks-max: 4 - maximum staggered mock duration in seconds
  • $ep-timers-min: 1 - minimum timer duration in seconds
  • $ep-timers-max: 4 - maximum timer duration in seconds
$ep-fg: #e74c3c3;
$ep-opacity: .8;
// import with default variables override
@import '~ep/core';

Sass mixin

ep also has a mixin available for coloring progress elements. Simply pass a color to color-bar. You may use this without even importing the ep reset if you just want to color some progress elements.

@import '~ep/mixins';

.my-progress-element { @include color-bar(#7bff7b); }

Javascript helper/API

ep also provides an optional Javascript helper/api. This can be used for convenience and also gives a little more control and power when interacting with <progress> elements. It doesn't create any extra elements, but you must pass a HTMLProgressElement into the constructor.

const bar = document.querySelector('progress');
const myEp = new Ep(bar);

It's main purpose is that it saves you the burden of having to set/remove attributes/classes. But it also provides some nice to haves such as being able to hook into when progress is complete or set.

The source is quite heavily documented and written with babel so be sure to check that out here.

As for the methods available(? denotes an optional parameter);

  • set({number} val, ? {function} cb) - Sets <progress> value with optional callback.
  • setSpread(? {bool} spread) - Set whether <progress> element should be spred style. By default will set to false.
  • setIndeterminate(? {bool} indeterminate) - Set whether <progress> element is using indeterminate helper class. By default, will remove helper class.
  • togglePause - Toggles pause attribute for play/pause animation.
  • setPosition(? {Array string} positions) - Takes an optional array of positions that will be applied to the element. For example, ['top', 'fixed'] will set ep--top ep--fixed class to the <progress> element. If no positions are declared, all currently applied will be wiped.
  • increase(? {number} value, ? {function} cb) - Increase progress value by optional increment with an optional callback. By default, increment is 5.
  • decrease(? {number} value, ? {function} cb) - Decrease progress value by optional decrement with an optional callback. By default, decrement is 5.
  • reset - Resets <progress> value to 0.
  • mock(? {number} duration, ? {bool} staggered, ? {function} cb) - Mocks progress with a mocking animation. Optional duration in seconds. Optional staggered attribute defines which mock style use. Optional callback can be invoked when mock is complete. By default, duration is 4.
  • time(? {number} duration, ? {function} cb) - Timing mock for using element as timer. Optional duration in seconds. Optional callback can be invoked when timer is complete. By default, duration is 4.
  • simulate(? {number} step, ? {number} increment, ? {number} max) - Simulation on the Javascript side is an example where we have more control than we do with CSS. Set a simulation by declaring a step duration in ms, an increment and a max value for the simulation to reach. The default simulation will increment by 5 every second until the <progress> element has a value of 99.
  • complete(? {function} cb) - Complete a progress bar by setting value to 100 and then resetting it. Provide optional callback for when complete.

Hooking into network requests

Yep. You can easily integrate ep to communicate network request status to the end user. The most basic example being something like the following;

  const myEp = new Ep(document.querySelector('progress'));
  const makeRequest = () => {
    myEp.simulate();
    const xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        myEp.complete();
      }
    };
    xhttp.open('GET', '/index.html', true);
    xhttp.send();
  }
  (myEp._VALUE) ? myEp.set(0, makeRequest) : makeRequest();

We start with a simple progress element. Reset it to make sure it starts at 0 and start a simulation. When we get the OK from our network request, set our element to complete 🎉

What happened to progrecss?

For some time, I'd intended to revisit progre(c)ss with some ideas I had. When I finally got round to it, I went back over the issues and something struck me. Someone had pointed out why not use the <progress> element?

I'd previously struck this off because I liked being able to add :pseudo element progress bars to any element with relative ease where the :pseudo elements were available.

However, using :pseudo elements to display progress isn't ideal and not very semantic.

It makes more sense to create something that can be integrated without big changes.

progre(c)ss is still available under the release tab if you really want it but realistically the code for progre(c)ss is as simple as;

.progrecss {
  &:before {
    color: green;
    content: '';
    height: 6px;
    left: 0;
    opacity: .8;
    position: absolute;
    top: 0;
  }
  @for $percent from 1 through 100 {
    &[data-progrecss-value='#{$percent}'] {
      &:before {
        width: $percent * 1%;
      }
    }
  }
}

Development

ep is developed using webpack, webpack-dev-server, babel and sass.

It uses a self-documented Makefile for development.

See available tasks

  make

Setup

  make setup

Start developing

  make develop

Contributing

Don't hesitate to post and issue, PR or suggestion. Alternatively, get in touch via email or by tweeting me @_jh3y! 😄

License

MIT


Made real by @jh3y 2017 😎

More Repositories

1

whirl

CSS loading animations with minimal effort!
SCSS
1,756
star
2

tyto

manage and organise things
JavaScript
675
star
3

driveway

pure CSS masonry layouts
HTML
632
star
4

vincent-van-git

Use your Github commit history as a canvas!
JavaScript
288
star
5

meanderer

A JavaScript micro-library for responsive CSS motion paths! ✨
JavaScript
223
star
6

sketchbook

The home for all my demo source wherever you may find them! ⚡️
HTML
183
star
7

gulp-boilerplate

a simple gulp boilerplate
JavaScript
146
star
8

kody

.files and environment configuration manager created with node
JavaScript
139
star
9

doormat

Let's take a scroll!
JavaScript
110
star
10

jh3y

73
star
11

pxl

A React app for drawing pixel art
JavaScript
72
star
12

osaka-card

TypeScript
59
star
13

tips

CSS tooltips!
CSS
47
star
14

jhey.dev

Site source for jhey.dev ✨
JavaScript
47
star
15

a-guide-to-css-animation

Demo code for "A Guide to CSS Animation"
CSS
44
star
16

use-caret-position

A custom React hook for grabbing the caret position in a text input.
JavaScript
37
star
17

parcel-boilerplate

a boilerplate for using Parcel JS 😎
HTML
30
star
18

culr

color search engine
JavaScript
27
star
19

node-cli-boilerplate

a simple boilerplate to get up and running with node cli apps
JavaScript
27
star
20

creatives-directory

A list of creatives to follow on Twitter 🐦
JavaScript
23
star
21

view-transition-sandbox

Sandbox to get creative with the View Transition API
Astro
20
star
22

my-script-kit

JavaScript
20
star
23

move-things-with-css

The accompanying source for "Move Things With CSS"
CSS
20
star
24

focussed-twitter

Let's focus on the tweets! 🐦
JavaScript
17
star
25

subscription-calendar-view-transitions

MPA View Transitions Subscription Calendar
Astro
17
star
26

stationery-cabinet

The repository for the majority of my CodePen creations!
Pug
15
star
27

crayon

a repo for styling CodePen profiles
HTML
14
star
28

deck

Astro
14
star
29

pen-case

A local development boilerplate set up for creating new pens 📝
Makefile
12
star
30

sike

a cli app that reminds you to move around every once in a while!
JavaScript
11
star
31

russ

a node scripts runner 🐰
JavaScript
11
star
32

parcel-react-boilerplate

a boilerplate for bundling react applications with Parcel JS
JavaScript
10
star
33

netlify-cms-next-starter

Get up and running with Netlify CMS && Next.js
JavaScript
10
star
34

next-portfolio-starter

A portfolio starting point using Next.js, Netlify CMS, Storybook, and Tailwind
JavaScript
9
star
35

kody_env

My personal settings for kody .files manager
Shell
9
star
36

serverless-og

JavaScript
9
star
37

whac-a-mole-react

JavaScript
9
star
38

dynamic-island

An Astro playground for building a "Dynamic Island" for your site
CSS
9
star
39

round-table

CSS
8
star
40

egghead

a home for my egghead material source 🥚
HTML
7
star
41

campfire

CSS
6
star
42

shooting-stars

HTML5 Canvas play around to create shooting stars effect
JavaScript
6
star
43

houdini-tesla

A CSS Houdini PaintWorklet for Tesla Coils
JavaScript
5
star
44

create-bangle-app

create and develop bangle.js apps from the command line
JavaScript
5
star
45

houdini-noise

CSS Houdini PaintWorklet for background noise
JavaScript
4
star
46

color-image-search-api

JavaScript
4
star
47

astro-mpa-bars

Quick POC for Astro Page Stinger View Transitions
TypeScript
4
star
48

which-key

an app for grabbing JavaScript KeyboardEvent info!
JavaScript
4
star
49

blurbs

Write something about me!
3
star
50

testPlayground

a repo for examples on how to set up mocha
JavaScript
3
star
51

jheymote

Audience remote for Jhey's conference talks
JavaScript
3
star
52

storybook-addon-studybook

Spin your Storybook into a learning aid with trackable progress.
JavaScript
3
star
53

key-book

create frozen enum objects from a string array ❄️
JavaScript
3
star
54

electron-parcel-react-starter

A starter project using Electron, React, and Parcel
JavaScript
3
star
55

border-radius-playground

An interactive visualisation tool for becoming a wizard with border-radius ✨
JavaScript
3
star
56

feedy

an angular app for consuming public photo feeds
JavaScript
3
star
57

inspector-gamut

A POC app for using AI to generate color palettes based on keywords
TypeScript
3
star
58

ng-mega-table

A directive for handling mega data
JavaScript
2
star
59

green-blocks

A Github Activity Visualizer Built with Next.js, React-Three-Fiber, Netlify, Tailwind, and GSAP
JavaScript
2
star
60

react-waves

A React component for making those fancy animated waves!
JavaScript
2
star
61

corvid

interactive command line Github API browser and cloning aid
JavaScript
2
star
62

blokky-road

3D CSS studio built with React on Vite
JavaScript
2
star
63

blink

JavaScript
2
star
64

fusion-hn

Hacker News example app built w/ Fusion JS
JavaScript
2
star
65

html5-boilerplate-jade-less-livereload.docpad

A skeleton for docpad using html5 boilerplate, less and jade.
CoffeeScript
1
star
66

interactive-twitch-overlay

Interactive Twitch Overlays Built with Tailwind && Next.js
JavaScript
1
star
67

sticky

A configurable sticky note component.
JavaScript
1
star
68

nodivember

CSS
1
star
69

blok-party

JavaScript
1
star
70

og-generator

A CLI tool for generating jh3y cover assets.
JavaScript
1
star
71

clinic

Submit your questions and issues for the clinic!
1
star
72

jheytompkins.com

source for
JavaScript
1
star
73

pencil-sharpener

Repo for storing my CodePen profile styling
HTML
1
star
74

next-storyblok-playground

JavaScript
1
star
75

gulp-template-store

a gulp plugin for storing lodash templates in a namespace object
JavaScript
1
star