Styling

The palette draws inside its shadow root, so page CSS does not leak into it. Three things reach in on purpose: a dark class, CSS custom properties, and shadow parts. The theming example shows three looks built from them.

Dark mode

Add the dark class to the element:

<ninja-keys class="dark"></ninja-keys>

To follow the page or the system, toggle the class from script:

const const dark: MediaQueryListdark = function matchMedia(query: string): MediaQueryList

The Window interface's matchMedia() method returns a new MediaQueryList object that can then be used to determine if the document matches the media query string, as well as to monitor the document to detect when it matches (or stops matching) that media query.

MDN Reference

matchMedia
('(prefers-color-scheme: dark)');
const const follow: () => booleanfollow = () => const ninja: NinjaKeysninja.Element.classList: DOMTokenList

The read-only classList property of the Element interface contains a live DOMTokenList collection representing the class attribute of the element. This can then be used to manipulate the class list.

MDN Reference

classList
.DOMTokenList.toggle(token: string, force?: boolean): boolean

The toggle() method of the DOMTokenList interface removes an existing token from the list and returns false. If the token doesn't exist it's added and the function returns true.

MDN Reference

toggle
('dark', const dark: MediaQueryListdark.MediaQueryList.matches: boolean

The matches read-only property of the MediaQueryList interface is a boolean value that returns true if the document currently matches the media query list, or false if not.

MDN Reference

matches
);
const dark: MediaQueryListdark.MediaQueryList.addEventListener<"change">(type: "change", listener: (this: MediaQueryList, ev: MediaQueryListEvent) => any, options?: boolean | AddEventListenerOptions): void (+1 overload)

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

MDN Reference

addEventListener
('change', const follow: () => booleanfollow);
const follow: () => booleanfollow();

CSS custom properties

Every colour and most sizes are custom properties set on the element, so a rule on ninja-keys (or a class on it) overrides them:

ninja-keys {
  --ninja-width: min(40rem, 100vw - 2rem);
  --ninja-top: 12vh;
  --ninja-accent-color: rebeccapurple;
  --ninja-font-family: inherit;
}

The API reference lists all of them with their defaults. The ones people change most:

PropertyDefaultFor
--ninja-width640pxthe palette’s width
--ninja-top20%its distance from the top
--ninja-font-size16pxthe base every size scales from
--ninja-font-familyinheritedthe font
--ninja-accent-colorpurplecaret, selection bar, focused chips
--ninja-modal-backgroundwhitethe panel
--ninja-overflow-backgroundtranslucent whitethe overlay over the page
--ninja-backdrop-filternonee.g. blur(4px) behind the overlay
--ninja-actions-height300pxthe list’s maximum height
--ninja-z-index1raise it above your own fixed headers

To match a site’s theme, point the properties at the site’s own variables, as this site does:

ninja-keys {
  --ninja-text-color: var(--color-ink);
  --ninja-modal-background: var(--color-surface);
  --ninja-accent-color: var(--color-accent);
  --ninja-secondary-text-color: var(--color-ink-dim);
}

The variables resolve in the page’s scope, so the palette follows the site’s light and dark themes without its own dark class.

Shadow parts

For anything the properties do not cover, style the parts with ::part():

PartIs
actions-listthe scrolling list
ninja-actioneach row
ninja-selectedthe selected row (it also has ninja-action)
ninja-icona Material icon
ninja-inputthe search input
ninja-input-wrapperthe box around the input
ninja-search-hintthe line under the input with completions and the notice
ninja-suggestionone completion chip
ninja-noticethe searcher’s notice
ninja-keys::part(actions-list) {
  padding: 0.5rem;
}

ninja-keys::part(ninja-action) {
  border-radius: 0.5rem;
  border-inline-start: none;
}

ninja-keys::part(ninja-selected) {
  background: color-mix(in oklch, currentColor 8%, transparent);
}

ninja-keys::part(ninja-input)::placeholder {
  font-style: italic;
}

::part() is supported in every current browser.

The footer with key hints is a slot. Put your own content in it, or an empty element to remove it:

<ninja-keys>
  <div slot="footer">Enter to run · Esc to close</div>
</ninja-keys>

Slotted content is in the page, not the shadow root, so page CSS styles it directly.

Custom icons

SVG icons in an action’s icon take the ninja-icon class to be sized by --ninja-icon-size and coloured by --ninja-icon-color; give the SVG stroke="currentColor" or fill="currentColor" so the colour applies. See Actions and menus.