• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 7 years ago
  • Updated almost 5 years ago

Reviews

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

Repository Details

A simple vanilla JavaScript off-canvas menu.

Canvi - A simple vanilla JavaScript off-canvas navigation

Official page

Introduction, installation

Canvi is a simple off-canvas navigation with a few parameter to configure. You can init multiple instances, set responsive width, use your style our Canvi's style.

To install Canvi get the JS and the CSS file and insert into your project. You can also customize the Sass to save some byte.

// To the head
<link rel="stylesheet" href="dist/canvi.css">
// To the end of the body
<script src="dist/canvi.js"></script>

You can also use Canvi in webpack based projects using canvi-webpack.js with import.

import Canvi from 'dist/canvi-webpack';

Basic Usage

To use Canvi first, you have to implement the correct HTML structure which is mean the proper use of the .canvi-navbar and .canvi-content.

<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
    <div class="canvi-navbar"></div>
    <div class="canvi-content"></div>
</body>
</html>

After this you need to call the Canvi constructor and viola you are done!

var canvi = new Canvi();

Multiple Instance

To make multiple instances, you must specify the correct value for the content, navbar and openButton property. Note that the content can be the same on multiple initializations.

var canviLeft = new Canvi({
    content: '.js-canvi-content',
    navbar: '.js-canvi-navbar--left',
    openButton: '.js-canvi-open-button--left'
});

var canviRight = new Canvi({
    content: '.js-canvi-content',
    navbar: '.js-canvi-navbar--right',
    openButton: '.js-canvi-open-button--right'
});

Accessibility

Canvi using aria-hidden="true" when the navigation is hidden. If you want to be more accesible you can use the inert attribute - which is also used by Canvi - but for this you need to use this polyfill because it is quite a new feature. Just simply insert the script into your project.

If you like to know more about inert check out A11ycats #2 episode!

Options

content

Type: string

Default: '.canvi-content'

This selector defines the content part of the markup. Mostly you need to modify this when you use multiple Canvi.

isDebug

Type: boolean

Default: false

Canvi's debugger switch.

navbar

Type: string

Default: '.canvi-navbar'

Define the navbar part of the structure. This selector is part of the default markup. Mostly you need to modify it'value when you use multiple Canvi.

speed

Type: string

Default: '0.3s'

You can specify here the opening/closing speed of the menu.

openButton

Type: string

Default: '.canvi-open-button'

Canvi open button selector.

position

Type: string

Default: 'left'

Specify the position of the navigation.

pushContent

Type: boolean

Default: true

Set if you don't want the menu to push the content. In this case, it will lay over your page.

width

Type: string

Default: '300px'

Set the width of the navbar. You must give string with the correct unit so you can specify any value type like em, %, px.

responsiveWidths

Type: array

Default: []

With this option, you can modify the width of the navbar by breakpoints. This is a mobile first approach, and the very first width option will be width parameter's value.

Methods

Canvi comes with a few method so you can open and close it manually from your code. To use these methods, you must save the Canvi instance in a variable at the declaration like to following:

var canviRight = new Canvi({
    content: '.js-canvi-content',
    navbar: '.js-canvi-navbar--right',
    openButton: '.js-canvi-open-button--right',
    position: 'right',
    pushContent: true,
    width: '300px'
});

open()

Open the Canvi instance.

canviRight.open();

close()

Close the Canvi instance.

canviRight.close();

toggle()

Toggle the Canvi instance.

canviRight.toggle();

Events

Canvi came with custom events which you can use to customize, modify Canvi or your site's specific parts. To catch an event all you have to do is to define an event listener to the body element.

Each event gives back three additional Canvi elements named by navbar, openButton, content. You can access them through the event object like e.detail.navbar.

canvi.init

This event is triggered when Canvi initialization ready. To catch the initialization event, you have to watch the canvi.init event. Note that you must declare the following code before the Canvi constructor.

document.querySelector('body').addEventListener('canvi.init', function(e) {
    console.log('Catch Canvi init event...');
});

canvi.before-open

This event is triggered when you click on the menu open button. To catch the before-open event, you have to watch the canvi.before-open event.

document.querySelector('body').addEventListener('canvi.before-open', function(e) {
    console.log('Catch Canvi before-open event...');
});

canvi.after-open

This event is triggered after the menu opened. To catch the after-open event, you have to watch the canvi.after-open event.

document.querySelector('body').addEventListener('canvi.after-open', function(e) {
    console.log('Catch Canvi after-open event...');
});

canvi.before-close

This event is triggered before you close the menu. To catch the before-close event, you have to watch the canvi.before-close event.

document.querySelector('body').addEventListener('canvi.before-close', function(e) {
    console.log('Catch Canvi before-close event...');
});

canvi.after-close

This event is triggered after the menu closed. To catch the after-close event, you have to watch the canvi.after-close event.

document.querySelector('body').addEventListener('canvi.after-close', function(e) {
    console.log('Catch Canvi after-close event...');
});

More Repositories

1

blade-filters

Use filters easily in your blade templates.
PHP
487
star
2

bazar

Bazar is an e-commerce package for Laravel applications.
PHP
421
star
3

sprucecss

An open-source, lightweight and modern CSS framework, design system built on Sass. Give your project a solid foundation.
SCSS
212
star
4

i18n

Push your Laravel translations to the front-end and use them easily with JavaScript.
PHP
123
star
5

policy

Using Laravel's authorization on the front-end.
PHP
97
star
6

simplepay-gateway

OTP SimplePay bővítmény WooCommerce (WordPress) áruházak részére.
PHP
57
star
7

sprucecss-eleventy-documentation-template

A simple documentation template made with Eleventy and Spruce CSS.
SCSS
50
star
8

bite-sized-a11y

Quick and Small Accessibility - Learn something new about accessibility with short and solid articles (less than 150 words).
CSS
28
star
9

root

Root is an admin package for Laravel applications.
PHP
20
star
10

wp-theme-alpha

Alpha is a WordPress blog and magazine theme with clean design, nice code, and Gutenberg support.
CSS
18
star
11

sprucecss-root-admin-template

A static HTML/CSS/JS administration tempalte built on Spruce CSS.
HTML
8
star
12

accessibility-path

Accessibility Path is a collection of resources that can help you to learn web accessibility and inclusive design.
7
star
13

sprucecss-starter-kit

The “Spruce CSS Starter Kit” repository contains a simple foundation for starting your next project with ease using Spruce CSS.
SCSS
6
star
14

billingo-php

Billingo PHP SDK.
PHP
5
star
15

sprucecss-site

The documentation of Spruce CSS.
MDX
5
star
16

bazar-docs

The Bazar Laravel e-commerce package documentation.
4
star
17

wordpress-plugin-template

WordPress plugin skeleton template.
PHP
4
star
18

qkie

Simple cookie management.
JavaScript
3
star
19

laravel-package-template

Laravel package skeleton template.
PHP
3
star
20

sprucecss-eleventy-starter

A minimalistic Eleventy boilerplate setup with Spruce CSS to help you start with your next project.
CSS
3
star
21

cone-site

The new 11ty-based portfolio website of Cone.
CSS
3
star
22

sprucecss-site-eleventy

The documentation of Spruce CSS.
CSS
3
star
23

sprucecss-examples

Simple examples for how to use and how to get started with Spruce CSS.
CSS
2
star
24

bazar-stripe

The Stripe Payment gateway for Bazar.
PHP
2
star