0.0.11 • Published 4 years ago

spotlight.js-proto v0.0.11

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Spotlight.js

Spotlight.js is a Javascript library that let developers highlight content on websites; it allows to create a tour, containing several elements the user can browse through.

With Spotlight.js, you could:

  • showcase your web app features;
  • guide users wanting to accomplish a given task;
  • attract users' attention on a given element.
  • do much more stuff!

Architecture

The library works the same way as a theatre set!

To perform a theatre play, you need actors, right? Well, same idea here! Each component you want to highlight can be declared as follows:

// actors need an asynchronous function that will return targeted HTMLElements.
const titleActor = new Actor(async () => document.getElementById('header-title'));
const menuActor = new Actor(async () => document.getElementById('right-menu'));

Of course, you need a spotlight to showcase them!

const spotlight = new Spotlight();

Then, add your actors as targets for your spotlight:

spotlight.addActors( titleActor, menuActor );

Options

You can set options:

  • for your Spotlight instance;
  • for your Actors, if you want to set settings for specific actors; these will override options set up in your Spotlight instance.
const spotlight = new Spotlight({ options });       // see available options below
spotlight.addActors(
    new Actor(async () => document.getElementById('paragraph-1'), { options }),
    new Actor(async () => document.getElementById('paragraph-2'))
); 

Spotlight options

namedescriptiondefault
allowHiddenFocusallows Spotlight.js to focus elements even when the spot is not activatedfalse
autoScrollwhen an actor is not in the viewport (and spot is on), scrolls the document until the actor is visibletrue
centerActorOnFocuswhen an actor is focused, scrolls the view to center it, even if it is completely in the viewporttrue
loopsat the end of the tour, focuses on the first actorfalse
onBackgroundClickif preventContentClicking is true, set a callback to call when the user clicks the page during the tour(spotlight) => spotlight.toggleSpot()
onEndcallback triggered when calling next() while last actor is focused() => console.log("tour ended")
onLightsOfftriggered when the spot goes off() => {}
preventContentClickingprevents users from clicking page content when a presentation is in progresstrue
preventFocusSpamblocks focus transition when another is runningtrue
resetOnLightsOfffocuses the first actor when turning the spot offfalse
resetOnEndspot fades off when next() is called while last actor is focusedtrue

Spotlight + Actor options

namedescriptiondefault
delaytransition time, in milliseconds, for the spot to switch from an actor to another1000
matchRadiusreproduces the border-radius of the targeted actortrue
onFocuscallback triggered when the spot starts targeting a new actor(newActor: Actor, oldActor: Actor) => { console.log('switching focus from ${oldActor} to ${newActor}'); }
onFocusedcallback triggered when the spot targets a new actor(actor: Actor) => { console.log('now focusing ${actor}'); }
shadowColorcolor of the shadow (you might want to put some alpha value here)#ff0000dd
showSignshould display the signtrue
showControlsshould display controls on the signtrue
signHTMLoverrides sign body with custom HTML code''
signPositiondefines position of the sign regarded the focused actor (can be TOP, RIGHT, BOTTOM, LEFT)SignPosition.RIGHT
signTexttext of the sign"Hello there!"
signTitletitle of the sign"Title"
validScrollContainercheck the "Limit parent scrolling" section below() => true

Tips

Actors with no target

If you want to center the sign on the screen with no particular target (eg for beginning a tour), provide () => null as builder to the Actor instance :

new Actor (
    async () => null, 
    {
        signTitle: 'Welcome to Spotlight.js', 
        signText: 'Since no actor has been provided, the sign centers itself on the page by default.'
    }
)

Limit parent scrolling

Under the hood, spotlight.js uses the scrollIntoView library to scroll to an element that is offscreen. When focusing an actor, it will attempt to scroll all elements containing the targeted element, which can lead to undesired behaviors, such as scrolling your app's main wrapper:

Parent overscrolling example
app wrapper is scrolled, revealing a white blank on the page bottom

To prevent this, you can control which elements scrollIntoView can scroll by providing the validScrollContainer option as such:

this.spotlight = new Spotlight ({
    // not scrolling the window nor #app container
    validScrollContainer: (target, parentsScrolled) => {
        return target !== window && !target.matches('#app');
    }
});
No parent overscrolling with the validScrollContainer function set
app wrapper is not a valid scrolling target anymore!

Check scrollIntoView's github for more details.

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago