littlefoot.js

versionminified + gzipcodecovCode Climate

littlefoot is a lightweight JavaScript library that creates exceptional footnotes.

Screen recording of littlefoot in action

Simply include the code on your pages and footnotes will be detected automatically and improved in the following ways:

This project includes the script itself and a default style to apply to the footnote buttons and popovers that are eventually generated. There are also a variety of additional styles that illustrate some of the possibilities for styling these components.

littlefoot was forked fromBigfoot.js byChris Sauvé. Unlike Bigfoot.js, littlefoot does not require jQuery.

Installation

Install using NPM:

npminstall--save littlefoot

Install using Yarn:

yarn add littlefoot

Usage

The script will work with a wide array of markup, but you will need to make sure that your footnote content/link markup at least resembles the markup shown below, which is the format generated by MultiMarkdown:

<!-- Links --><p><supid="fnref:1"><ahref="#fn:1">1</a></sup></p><!-- Footnote List --><divclass="footnotes"><ol><liclass="footnote"id="fn:1"><p>footnote.<ahref="#fnref:1"title="return to article"></a></p><p></p></li></ol></div>

Once you’ve set up the appropriate markup, all you need to do is include the following in your code:

const{littlefoot}=require('littlefoot')littlefoot()

You can also configure the available options by passing an object literal, and you can store the return object to make use of some of the methods it makes available:

const{littlefoot}=require('littlefoot')constlf=littlefoot({activateOnHover:true,hoverDelay:250,})

You’ll also want to include styles for the button and popovers, a number of which come with the script.

Usage withWordPress

Install theLittlefoot for Footnotes plugin by following the provided instructions.

Usage withGatsby

  1. Install littlefoot as a dependency in your Gatsby project.

  2. At the root of the site’s project, create or edit agatsby-browser.js file and add the following:

    importlittlefootfrom'littlefoot'import'littlefoot/dist/littlefoot.css'exportfunctiononRouteUpdate(){littlefoot()// Pass any littlefoot settings here.}

Usage from a CDN

Follow these steps to add littlefoot to a plain HTML document without packagemanagement support. While this method is simpler to use, bear in mind thatincluding files from external sources will slightly degrade the page’s loadingperformance.

  1. Add the required footnote stylesheet at the top of your HTML file, inside the<head> tag:

    <linkrel="stylesheet"href="https://unpkg.com/littlefoot/dist/littlefoot.css"/>
  2. Add the following script tags at the end of the document, just before theclosing</body> tag:

    <scriptsrc="https://unpkg.com/littlefoot/dist/littlefoot.js"type="application/javascript"></script><scripttype="application/javascript">littlefoot.littlefoot()// Pass any littlefoot settings here.</script>

Seean example on CodePen.

Options

The script has many configurable options from having popovers instantiated on hover, to allowing multiple active footnotes, to setting specific timeouts for popover activation and dismissal. It also returns an object that allows you to activate and dismiss popovers.

activateCallback

Specifies a function to call on a footnote popover that is being activated (after it is added to the DOM). The function will be passed two arguments: thepopover DOM element, and thebutton that was activated to show the popover. This option can be useful for adding classes or styling information on the popover.

Default:undefined

dismissCallback

Specifies a function to call on a footnote popover that is being dismissed (just before it is removed from the DOM). The function will be passed two arguments: thepopover DOM element, and the relatedbutton. This option can be useful for removing classes on the popover.

Default:undefined

activateDelay

Sets a delay between the activation of the footnote button and the activation of the actual footnote content.

Default:100

activateOnHover

Specifies whether or not the footnote content will be activated when the associated button is hovered over.

Default:false

allowDuplicates

Determines whether or not a footnote can be used as the content for multiple footnote buttons. Many content management systems will, on a blog’s main page, load every article chronologically without any adjustments to the article markup. This can cause issues if multiple footnotes have the same ID: the footnote content is identified by the fragment identifier in thehref attribute of the footnote link, so multiple identical IDs can result in the same footnote content being used for different footnote links. This option prevents this by using a footnote as the content for at most one footnote button.

Default:true

allowMultiple

Specifies whether or not multiple footnote popovers can be active simultaneously.

Default:false

anchorPattern

Specifies the pattern that must be matched by the anchor element’shref attribute for it to be considered a footnote link. This is used in filtering all links down to just those with a footnote.

Default:/(fn|footnote|note)[:\-_\d]/gi

anchorParentSelector

The selector for the parent of the footnote link. This is really only necessary when you want to get rid of that element — for instance, when the link is inside asup tag. This tag and the link itself will be joined together for attribute from which you can drawn in your markup for footnotes/buttons.

Default:sup

dismissDelay

When the footnote content is being removed this option specifies how long after the active class is removed from the footnote before the element is actually removed from the DOM.

Default:500

dismissOnDocumentTouch

Determines whether touching the document will dimiss all active footnotes. Whenfalse, footnotes can be dismissed by touching the button again.

Default:true

dismissOnUnhover

Determines whether footnotes that were presented when hovering on a footnote button are dismissed once the footnote button or footnote popover is un-hovered.

Default:false

footnoteSelector

The element that contains the footnote content. This element will be hidden and given alittlefoot--print class once littlefoot has finished with it.

Default:'li'

hoverDelay

IfdismissOnUnhover is true, this specifies the amount of time (in milliseconds) that must pass after the footnote button/content is un-hovered before the footnote is dismissed.

Default:250

numberResetSelector

A string representing the selector at which you would like the numbering of footnotes to restart at 1. For example, you may be using the numbered style of footnote and wish to have the numbers restart for each<article> on your main page with a class of'article-container' In this case, you would set this option to'article.article-container' (or an equivalent CSS selector). Leaving the option undefined will simply number all footnotes on a given page sequentially.

Default:undefined

scope

If any truthy value is provided, only the footnotes within the scope you define will be affected by the script. The scope should be a selector string, as you would typically use in jQuery. For example, setting a scope of'.littlefoot-active' would work only on those elements with an ancestor that has a class oflittlefoot-active.

Default:undefined

contentTemplate

A template for the markup of the footnote content popovers. It’s best not to change this too much; the library relies on the class names and hierarchy of the default markup to do its work. However, you can add information to the rendered markup by adding string literals or one or more of the following variables:

Variables should be added between interpolation delimiters. For example,<% content %>.

Default:

<asidealt="Footnote <% number %>"class="littlefoot__popover"id="fncontent:<% id %>"><divclass="littlefoot__wrapper"><divclass="littlefoot__content"><%content%></div></div><divclass="littlefoot__tooltip"></div></aside>

buttonTemplate

A template for the markup of the footnote button. Again, try not to remove any elements from the markup, but add as much as you like.

Variables should be added between interpolation delimiters. For example,<% content %>.

Default:

<buttonclass="littlefoot__button"id="<% reference %>"title="See Footnote <% number %>"><svgrole="img"aria-labelledby="title-<% reference %>"viewbox="0 0 31 6"preserveAspectRatio="xMidYMid"><titleid="title-<% reference %>">Footnote<%number%></title><circler="3"cx="3"cy="3"fill="white"></circle><circler="3"cx="15"cy="3"fill="white"></circle><circler="3"cx="27"cy="3"fill="white"></circle></svg></button>

Numerical footnotes

To display the footnote number instead of an ellipsis, provide the followingbuttonTemplate instead:

<buttonaria-label="Footnote <% number %>"class="littlefoot__button"id="<% reference %>"title="See Footnote <% number %>"/><%number%></button>

Methods

Running the function will return an object that can be stored and used to manipulate the footnote buttons/content. The following methods are available in this return object:

dismiss([footnoteId[, timeout]])

This function will close the popover matching the footnote ID. If omitted, all popovers are dismissed.timeout specifies the amount of time after the footnote’s active class is removed before the element itself is removed. If excluded,timeout will default to thedismissDelay option.

activate(footnoteId[, timeout])

This will activate the footnote button (and its associated popover) matching thefootnote ID.timeout specifies the amount of time before the footnote’s activeclass is applied. If excluded,timeout will default to theactivateDelayoption.

getSetting(key)

Returns the instance setting matching the provided string key.

updateSetting(key, value)

Updates the littlefoot instance settings for the provided string key with a newvalue.

unmount()

This will disable littlefoot and restore the document to its original structure,clearing event handlers. Onceunmount() is called, all other methods providedin the littlefoot instance will stop working, requiring you to call thelittlefoot() function again.

Theming

littlefoot supports theming throughCSS custom properties. The following custom properties are available in browsers orCSS precompilation tools that support them, and are scoped to the.littlefoot class.

Example

Customise littlefoot’s appearance by overriding its custom properties in your site’s CSS, like so:

.littlefoot{--button-active-background-color:#d2b450;--button-text-color:#fff;}

Buttons

Button propertiesDefault valuePurpose
--button-background-color#949494Button background color.
--button-active-background-color#4c4c4cActive button background color.
--button-border-radius0.5remButton border radius.
--button-active-text-color#fafafaActive button text color.
--button-height1remButton height.
--button-margin0 0.1remButton margin.
--button-padding0 0.6remButton padding
--button-text-color#fafafaButton text color.
--button-transitionbackground-color 0.25s ease, color 0.25s easeButton transition animation.

Popovers

Popover propertiesDefault valuePurpose
--popover-background-color#f5f5f5Popover background color.
--popover-border-radius0.5remPopover border radius.
--popover-border1px solid #949494Popover border.
--popover-font-familyinitialPopover text font family.
--popover-font-sizeinitialPopover text font size.
--popover-font-styleinitialPopover text font style.
--popover-font-weightinitialPopover text font weight.
--popover-horizontal-padding1.4remPopover horizontal padding.
--popover-line-heightnormalPopover text line height.
--popover-max-height15emMaximum popover height.
--popover-max-width90%Maximum popover width.
--popover-scroll-indicator-color#4c4c4cPopover scroll indicator color.
--popover-shadow0 0 8px rgba(0, 0, 0, 0.3)Popover drop shadow.
--popover-text-color#111Popover text color.
--popover-tooltip-size0.5remPopover tooltip size.
--popover-transform-origin50% 0Popover transform origin.
--popover-transformscale(0.1) translateZ(0)Initial popover transform.
--popover-active-transformscale(1) translateZ(0)Activated popover transform.
--popover-transitionopacity 0.25s ease, transform 0.25s easePopover transition animation.
--popover-vertical-padding0.6remPopover vertical padding.
--popover-width22emPopover width.

Backwards compatibility

Browsers that don’t support CSS custom properties will receive the default values. The appearance of elements may still be overridden through normal CSS definitions as in previous versions of littlefoot.

Changes from Bigfoot.js

Consult thechangelog.

License

MIT ©Chris Sauvé andLuís Rodrigues.