2.0.0 • Published 2 years ago

bs-darkmode v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

GitHub license Latest release JSDelivr Badge NPM Badge Maintenance Funding Rate this package

Bootstrap Darkmode

Bootstrap Darkmode is a bootstrap plugin/widget that supports toggling between light and dark theme.


Library Distributions

BranchBootstrap SupportLast Release
bs-darkmode v2.X.XBootstrap 5Latest release
bs-darkmode v1.X.XBootstrap 4Latest release

Demos

Demos and API Docs: https://palcarazm.github.io/bs-darkmode/

Table of Contents generated with DocToc


Installation

CDN

JSDelivr Badge

jQuery Interface

<link href="https://cdn.jsdelivr.net/npm/bs-darkmode@2.0.0/css/bs-darkmode.min.css" rel="stylesheet">  
<script src="https://cdn.jsdelivr.net/npm/bs-darkmode@2.0.0/js/bs-darkmode.jquery.min.js"></script>

ECMAS Interface

<link href="https://cdn.jsdelivr.net/npm/bs-darkmode@2.0.0/css/bs-darkmode.min.css" rel="stylesheet">  
<script src="https://cdn.jsdelivr.net/npm/bs-darkmode@2.0.0/js/bs-darkmode.ecmas.min.js"></script>

Download

Latest release

NPM

NPM Badge

npm install bs-darkmode@2.0.0

Yarn

yarn add bs-darkmode@2.0.0

Usage

Initialize With HTML

Simply add data-plugin="bsdarkmode" to automatically convert an element to a Bootstrap.

<button class="btn btn-primary" data-plugin="bsdarkmode"></button>

Initialize With Code

Toggles can also be initialized via JavaScript code.

EX: Initialize id darkmodeToggle with a single line of JavaScript.

<button class="btn btn-primary" id="darkmodeToggle"></button>
<script>
  $(function(){
    $('#darkmodeToggle').bsDarkmode();
  });
</script>

API

Options

  • Options can be passed via data attributes or JavaScript
  • For data attributes, append the option name to data- (ex: data-state="light")
<div id="api-example">
    <button class="btn btn-primary" data-plugin="bsdarkmode" data-state="dark" data-root="#api-example"
        data-lightvars='{"primary":"#FF0000","secondary":"#00FF00"}'
        data-darkvars='{"primary":"#0000FF","secondary":"#FFFF00"}'></button>
    <button class="btn btn-primary" id="darkmodeToggle"></button>
</div>
<script>
    $(function () {
        $('#darkmodeToggle').bsDarkmode({
            state: false,
            root:'#api-example',
            lightvars: '{"primary":"#FF0000","secondary":"#00FF00"}',
            darkvars: '{"primary":"#0000FF","secondary":"#FFFF00"}'
        });
    })
</script>
NameTypeDefaultDescription
statebooleantrueInitial state (For data-state use light or dark). The user preferred color scheme dark is prioritary.
rootstring":root"Root element to apply CSS vars
allowsCookiebooleanfalseCookie authorization status
lightlabelhtmlsun svgElement inner HTML for light mode
darklabelhtmlmoon svgElement inner HTML for dark mode
lightvarsstring"{}"JSON object with CSS vars for light mode
darkvarsstring"{}"JSON object with CSS vars for dark mode

CSS Vars to change

The following CSS Vars that can be changed form lightvars and darkvars attributes.

VarTypeDefault LigthDefault DarkDescription
blueHEX color#0d6efd#3f6791Standard Bootstrap color
indigoHEX color#6610f2#6610f2Standard Bootstrap color
purpleHEX color#6f42c1#6f42c1Standard Bootstrap color
pinkHEX color#d63384#e83e8cStandard Bootstrap color
redHEX color#dc3545#e74c3cStandard Bootstrap color
orangeHEX color#fd7e14#fd7e14Standard Bootstrap color
yellowHEX color#ffc107#f39c12Standard Bootstrap color
greenHEX color#198754#00bc8cStandard Bootstrap color
tealHEX color#20c997#20c997Standard Bootstrap color
cyanHEX color#0dcaf0#3498dbStandard Bootstrap color
grayHEX color#6c757d#ced4daStandard Bootstrap gray color
gray-darkHEX color#343a40#e9ecefStandard Bootstrap gray color
gray-100HEX color#f8f9fa#212529Standard Bootstrap gray color
gray-200HEX color#e9ecef#343a40Standard Bootstrap gray color
gray-300HEX color#dee2e6#495057Standard Bootstrap gray color
gray-400HEX color#ced4da#6c757dStandard Bootstrap gray color
gray-500HEX color#adb5bd#adb5bdStandard Bootstrap gray color
gray-600HEX color#6c757d#ced4daStandard Bootstrap gray color
gray-700HEX color#495057#dee2e6Standard Bootstrap gray color
gray-800HEX color#343a40#e9ecefStandard Bootstrap gray color
gray-900HEX color#212529#f8f9faStandard Bootstrap gray color
primaryHEX color#0d6efd#3f6791Standard Bootstrap theme color
secondaryHEX color#6c757d#ced4daStandard Bootstrap theme color
successHEX color#198754#00bc8cStandard Bootstrap theme color
infoHEX color#0dcaf0#3498dbStandard Bootstrap theme color
warningHEX color#ffc107#f39c12Standard Bootstrap theme color
dangerHEX color#dc3545#e74c3cStandard Bootstrap theme color
lightHEX color#f8f9fa#212529Standard Bootstrap theme color
darkHEX color#212529#f8f9faStandard Bootstrap theme color
body-colorHEX color#212529#f8f9faBody color
body-bgHEX color#fff#495057Body background color
whiteHEX color#fff#000White color
blackHEX color#000#fffBlack color

Theme colors, black and white are also exposed in HSL with {color}-h, {color}-s and {color}-land in RGB with {color}-rgb.

Methods

Methods can be used to control toggles directly.

<button class="btn btn-primary" id="darkmodeToggle"></button>
MethodExampleDescription
initialize$('#darkmodeToggle').bsDarkmode()Initializes the darkmode plugin with options
light$('#darkmodeToggle').bsDarkmode('light')Sets the darkmode toggle to 'light' state
dark$('#darkmodeToggle').bsDarkmode('dark')Sets the darkmode toggle to 'dark' state
toggle$('#darkmodeToggle').bsDarkmode('toggle')Toggles the state of the darkmode toggle light/dark
setCookieAutorization$('#darkmodeToggle').bsDarkmode('setCookieAutorization', status)Sets the Cookie authorization status. Status can be true or false

Events

Event Propagation

Note All events are propagated to and from the element to the darkmode toggle.

You should listen to events from the HTML element directly rather than look for custom events.

<button class="btn btn-primary" id="darkmodeToggle" data-plugin="bsdarkmode"></button>
<div id="console-event"></div>
<script>
  $(function() {
    $('#darkmodeToggle').change(function() {
      $('#console-event').html('Mode changed')
    })
  })
</script>

Stopping Event Propagation

Passing true to the light, dark and toggle methods will enable the silent option to prevent the control from propagating the change event in cases where you want to update the controls light/dark state, but do not want to fire the onChange event.

<button class="btn btn-primary" data-plugin="bsdarkmode" id="darkmodeToggle"></button>
<button class="btn btn-success" onclick="$('#darkmodeToggle').bsDarkmode('light',true)" >Light by API (silent)</button>
<button class="btn btn-success" onclick="$('#darkmodeToggle').bsDarkmode('dark',true)">Dark by API (silent)</button>
<button class="btn btn-warning" onclick="$('#darkmodeToggle').bsDarkmode('light')">Light by API (not silent)</button>
<button class="btn btn-warning" onclick="$('#darkmodeToggle').bsDarkmode('dark')">Dark by API (not silent)</button>

Collaborators welcom!

GitHub Contributors

¿Do you like the project? Give us a :star: in GitHub.