An open API service indexing awesome lists of open source software.

https://github.com/roydukkey/custom-highlight

Lifecycle hooks for styling arbitrary text within an HTML Element
https://github.com/roydukkey/custom-highlight

css lifecycle-hooks vue-directive

Last synced: 5 months ago
JSON representation

Lifecycle hooks for styling arbitrary text within an HTML Element

Awesome Lists containing this project

README

          

# `custom-highlight`

A set of lifecycle hooks for styling arbitrary text within elements using the [CSS Custom Highlight API](https://developer.mozilla.org/en-US/docs/Web/API/CSS_Custom_Highlight_API).

[![Release Version](https://img.shields.io/npm/v/custom-highlight.svg)](https://www.npmjs.com/package/custom-highlight)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

## Features

* Highlight **single or multiple tokens** within an element, or patterns using a **regular expression**
* Easily configure **case-insensitive** searching for tokens and patterns
* Define **custom highlight groups** for targeting each with unique styles
* Deeply monitor changes to character data triggering an update to highlights

## Install

```sh
npm install custom-highlight
```

## CDN

```html

```

It will be exposed globally as `window.CustomHighlight`.

## Usage

Here is a very basic example that will highlight the words “brown fox” in the paragraph.

```html

The quick brown fox jumps over the lazy dog.

import CustomHighlight from 'custom-highlight';

const element = document.getElementById('gettingStarted');
const options = { value: 'brown fox' };

if (element) {
CustomHighlight
.created(element, options)
.beforeMount(element, options)
.mounted(element, options);
}

::highlight(default) {
background-color: yellow;
color: black;
}

```

This works just fine in a static webpage, but frameworks that dynamically render content by manipulating the DOM (e.g. Vue, React, Svelte, etc.) will require additional effort in conforming to their component lifecycles.

See the [docs](https://custom-highlight.pages.dev/) for examples and recipes using [Vue](https://custom-highlight.pages.dev/recipes/vue.html) and native [Web Components](https://custom-highlight.pages.dev/recipes/web-component.html).