diff --git a/README.md b/README.md index f4d02c2..5878535 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Simple dark-mode/light-mode logic for any website [drkmd.js](https://github.com/BetaHuhn/drkmd.js) (short for darkmode.js) lets you add a dark-mode/light-mode toggle to any website. The library detects the system theme automatically and even saves the users choice in local storage or as a cookie. -The library will add the class `theme-dark`/`theme-light` to the body of the page and set the attribute `data-theme` to `dark`/`light` on the html tag which can be used to specify different css styles depending on the theme. You can also listen to a `theme-change` event for more advanced use cases. See [usage](https://github.com/BetaHuhn/drkmd.js#usage) below how you can customize your page with this. +The library will add the class `theme-dark`/`theme-light` to the body of the page and set the attribute `data-theme` to `dark`/`light` on the html tag which can be used to specify different css styles depending on the theme. You can also listen to a `theme-change` event for more advanced use cases. See [usage](#-usage) below how you can customize your page with this. ## 🚀 Get started @@ -51,9 +51,9 @@ npm install drkmd-js Then add the following JavaScript code: ```javascript -import Darkmode from 'drkmd-js'; +import Darkmode from 'drkmd-js' -new Darkmode().attach(); +new Darkmode().attach() ``` Both methods will add the [darkmode toggle](#default-theme-toggle) with all the default [options](#%EF%B8%8F-options) to your page. @@ -123,8 +123,8 @@ You can also use `data-drkmd-to-light` and `data-drkmd-to-dark` to switch to a s To enable/disable Darkmode you can use the method `toggle()`: ```javascript -const darkmode = new Darkmode(); -darkmode.toggle(); +const darkmode = new Darkmode() +darkmode.toggle() ``` There are also other methods available: @@ -144,18 +144,18 @@ darkmode.isLight() // Returns true if the current theme is light By default [drkmd.js](https://github.com/BetaHuhn/drkmd.js) emits a `theme-change` event everytime the theme changes: ```js -import Darkmode from 'drkmd-js'; +import Darkmode from 'drkmd-js' -new Darkmode(); +new Darkmode() window.addEventListener('theme-change', e => { - console.log(e.detail.to); // will return 'light' or 'dark' -}); + console.log(e.detail.to) // will return 'light' or 'dark' +}) ``` This can be turned off by setting the option `events: false`. -The `theme-change` event could be used to change the `src` attribute of an `` tag depending on the theme ([more info](https://github.com/BetaHuhn/drkmd.js/discussions/11#discussioncomment-247341)) or modify the page in any other way with JavaScript when the theme changes. +The `theme-change` event could be used to change the `src` attribute of an `` tag depending on the theme (example [below](#different-images-depending-on-the-theme)) or modify the page in any other way with JavaScript when the theme changes. ## ⚙️ Options @@ -267,7 +267,7 @@ Render the darkmode toggle with custom options: **HTML** ```html - + ``` or @@ -313,11 +313,15 @@ Then on your custom element add the attribute `data-drkmd-toggle`: Change theme ``` +When you click that element, the theme will be toggled automatically. + +There's also `data-drkmd-to-light` and `data-drkmd-to-dark` which will change the theme to either dark or light specifically. + --- ### Different images depending on the theme -You can use the `theme-change` event to modify an element with JavaScript. Here we are changing the `src` attribute of an `img` tag when the theme changes: +You can use the `theme-change` [event](#events) to modify an element with JavaScript. Here we are changing the `src` attribute of an `img` tag when the theme changes: **HTML** ```html diff --git a/examples/basic/index.html b/examples/basic/index.html new file mode 100644 index 0000000..f039844 --- /dev/null +++ b/examples/basic/index.html @@ -0,0 +1,37 @@ + + + + + + + Darkmode example + + + + +

Darkmode example

+ + + + + + + + \ No newline at end of file diff --git a/examples/css-variables/index.html b/examples/css-variables/index.html new file mode 100644 index 0000000..8c5200f --- /dev/null +++ b/examples/css-variables/index.html @@ -0,0 +1,43 @@ + + + + + + + Darkmode example + + + + +

Darkmode example

+ + + + + + + + \ No newline at end of file diff --git a/examples/custom-toggle/index.html b/examples/custom-toggle/index.html new file mode 100644 index 0000000..31dbc44 --- /dev/null +++ b/examples/custom-toggle/index.html @@ -0,0 +1,46 @@ + + + + + + + Darkmode example + + + + +

Darkmode example

+ Switch theme + + Light theme + Dark theme + + + + + + + + \ No newline at end of file diff --git a/examples/events/index.html b/examples/events/index.html new file mode 100644 index 0000000..76a7610 --- /dev/null +++ b/examples/events/index.html @@ -0,0 +1,56 @@ + + + + + + + Darkmode example + + + + +

Darkmode example

+ + + + + + + + + + + \ No newline at end of file diff --git a/examples/options/index.html b/examples/options/index.html new file mode 100644 index 0000000..12ae5f9 --- /dev/null +++ b/examples/options/index.html @@ -0,0 +1,50 @@ + + + + + + + Darkmode example + + + + +

Darkmode example

+ + + + + + + + \ No newline at end of file diff --git a/examples/programmatic/index.html b/examples/programmatic/index.html new file mode 100644 index 0000000..7927587 --- /dev/null +++ b/examples/programmatic/index.html @@ -0,0 +1,65 @@ + + + + + + + Darkmode example + + + + +

Darkmode example

+ + + + + + + + + + + + + \ No newline at end of file diff --git a/example/index.html b/test.html similarity index 100% rename from example/index.html rename to test.html