# snabbdom-selector

> Snabbdom CSS-Selector

Latest version **5.0.0** (published 2021-06-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install snabbdom-selector
pnpm add snabbdom-selector
yarn add snabbdom-selector
bun add snabbdom-selector
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2021-06-26 |
| First published | 2015-12-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 47.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 32 |
| Author | Tylor Steinberger |
| Maintainers | tylors, atomrc |
| Keywords | Snabbdom, CSS, Selector |

## Links

- npm: https://www.npmjs.com/package/snabbdom-selector
- Repository: https://github.com/tylors/snabbdom-selector
- Homepage: https://github.com/tylors/snabbdom-selector#readme
- Issues: https://github.com/tylors/snabbdom-selector/issues
- npm.io page: https://npm.io/package/snabbdom-selector

## Dependencies (1)

- [tree-selector](https://npm.io/package/tree-selector.md) ^2.1.0

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2021-06-26
- 4.2.0 — 2019-08-05
- 4.1.0 — 2018-10-29
- 4.0.0 — 2018-09-07
- 3.1.0 — 2018-08-31
- 3.0.0 — 2018-04-27
- 2.0.1 — 2017-10-26
- 2.0.0 — 2017-10-14
- 1.3.4 — 2017-10-12
- 1.3.3 — 2017-10-12
- 1.3.2 — 2017-08-04
- 1.3.0 — 2017-08-04
- 1.2.1 — 2017-07-18
- 1.1.2 — 2017-07-18
- 1.2.0 — 2017-03-22
- … 9 more at https://npm.io/package/snabbdom-selector/versions

## README

# Snabbdom Selector [![ComVer](https://img.shields.io/badge/ComVer-compliant-brightgreen.svg)](https://github.com/staltz/comver)

`snabbdom-selector` is a utility tool, written in TypeScript,
to find snabbdom VNode objects matching a given CSS Selector.

## This is for me!
```sh
npm install snabbdom-selector
```

## Basic Usage
```js
import { select } from 'snabbdom-selector'
import { h } from 'snabbdom'

const vNode = h('div', {}, [
  h('div.test', {}, [
    h('p', {key: 1}, 'Foo')
  ])
])

const matches = select('.test p', vNode)

console.log(matches)
// => [{sel: 'p', text: 'Foo', elm: HTMLElement, key: 1, children: undefined, data: {...}}]
```

## API

### `select(cssSelector: string, vNode: snabbdom.VNode): Array<snabbdom.VNode>`
### `select(cssSelector: string): (vNode: snabbdom.VNode) => Array<snabbdom.VNode>`

Note that there are 2 function signatures, this is because `select` is curried!

This is the main function provided by this library, it allows using a css selector to
find vNode's that are made from that css selector. The vNode used will be traversed, and
any matches at any arbitrary depth will be returned inside of the resulting array. An empty
array will be returned if no matches are found.

```js
import { select } from 'snabbdom-selector'

const vNode = h('div', {}, [
  h('div.test', {}, [
    h('p', {key: 1}, 'Foo')
  ])
])

const match = select('.test p'); // if given 1 parameter it returns a new function!
const matches = match(vNode);

console.log(matches)
// => [{sel: 'p', text: 'Foo', elm: HTMLElement, key: 1, children: undefined, data: {...}}]
```

### `selectorParser(selector: string): Object`

This function given a CSS selector like that passed to snabbdom's `h` function, returns an object
containing parsed tagName, id, and className.

```js
import { selectorParser } from 'snabbdom-selector'

const { tagName, id, className } = selectorParser('div#foo.bar.baz');

console.log(tagName) // div
console.log(id) // foo
console.log(className) // 'bar baz'
```

### `classNameFromVNode(vNode: snabbdom.VNode): string`

This function given a snabbdom VNode object will return its className including that contained
within snabbdom's provided `props` and `class` modules.

```js
import { classNameFromVNode } from 'snabbdom-selector'

const vNode = h('div.foo', { class: { bar: true } }, [])

console.log(classNameFromVNode(vNode)) // 'foo bar'
```

## Versioning
This library uses [compatible versioning](https://github.com/staltz/comver), a versioning system
that is backwards compatible with semantic-versioning.

---
_Source: https://npm.io/package/snabbdom-selector · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
