Spruce comes with a few JS utilities needed for the UI components or in generic development.

The scripts utilize JS modules, so you have to use import to use them.

The scripts utilize JS modules, so you have to use import to use them. One thing that is good to know here is that you need to specify the type="module" attribute and value for native browser support with import.

html
<script type="module" src="your-script-with-import.js">

A small utility to manage cookies from the client side (which is not straightforward in JS but sometimes needed).

setCookie

Set a cookie name-value pair with additional options.

Parameter(s)

NameDefault ValueDescription
keyThe name of the cookie.
valueThe value of the cookie.
expiresnullCookie expiration date in days.
path/The URL path where the cookie exists.
options{}}Any additional name and value pair.

Any cookie which you set gets two default settings:

  • SameSite: 'Lax',
  • Secure: true.

If you want to overwrite any of them, use the last, options parameter.

Example(s)

js
import {setCookie} from '~sprucecss/js/cookie.js';
setCookie('spruce-test', true, 10, '/');

getCookie

Get a cookie value by name.

Parameter(s)

NameDescription
keyThe name of the cookie.

Example(s)

js
import {getCookie} from '~sprucecss/js/cookie.js';
getCookie('spruce-test');

issetCookie

Check if a cookie exist by name.

Parameter(s)

NameDescription
keyThe name of the cookie.

Example(s)

js
import {issetCookie} from '~sprucecss/js/cookie.js';
issetCookie('spruce-test');

removeCookie

Remove a cookie by name.

Parameter(s)

NameDescription
keyThe name of the cookie.

Example(s)

js
import {removeCookie} from '~sprucecss/js/cookie.js';
removeCookie('spruce-test');