# dom-casque

> dom html tool

Latest version **1.1.0** (published 2024-02-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install dom-casque
pnpm add dom-casque
yarn add dom-casque
bun add dom-casque
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2024-02-28 |
| First published | 2019-04-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 30.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ilex |
| Maintainers | ilex.h |

## Links

- npm: https://www.npmjs.com/package/dom-casque
- npm.io page: https://npm.io/package/dom-casque

## Dependencies (2)

- [dt2react](https://npm.io/package/dt2react.md) ^2.x
- [prop-types](https://npm.io/package/prop-types.md) ^15.6.1

## Recent versions

- 1.1.0 (latest) — 2024-02-28
- 1.0.3 — 2022-06-06
- 1.0.2 — 2021-05-18
- 1.0.1 — 2019-04-29
- 1.0.0 — 2019-04-28

## README

# dom-casque

## usage

```js
import DomHtml, { withSideEffect } from 'dom-casque';
```

### basic use

```js
import React, { Component } from 'react';
import DomHtml from 'dom-casque';

export default class App extends Component {

  render() {
    return (
      <div className="application">
        <DomHtml>
          <meta charSet="utf-8" />
          <title>My Title</title>
          <link rel="canonical" href="http://mysite.com/example" />
        </DomHtml>
        ...
      </div>
    );
  }
}

```

### multiple instances

```js
<Parent>
  <DomHtml>
    <title>My Title</title>
    <meta name="description" content="DomHtml application" />
  </DomHtml>

  <Child>
    <DomHtml>
      <title>Internal changes Title</title>
      <meta name="description" content="Internal changes component" />
    </DomHtml>
  </Child>
</Parent>
```

outputs:

```html
<head>
    <title>Internal changes Title</title>
    <meta name="description" content="Internal changes component">
</head>
```

## apis

This `DomHtml` instance contains the following properties:

- `base`
- `bodyAttributes`
- `htmlAttributes`
- `link`
- `meta`
- `noscript`
- `script`
- `style`
- `title`

## Reference Guide

### as DomHtml attributes

### encodeSpecialCharacters

(optional) set to false to disable string encoding (server-only)

```js
<DomHtml
  encodeSpecialCharacters={true}
>
<title>Internal changes Title</title>
</DomHtml>
```

### titleTemplate

(optional) Useful when you want titles to inherit from a template:

```js
<DomHtml
  titleTemplate="%s | MyAwesomeWebsite.com"
>
<title>Internal changes Title</title>
</DomHtml>

// outputs
<head>
  <title>Internal changes Title | MyAwesomeWebsite.com</title>
</head>
```

### defaultTitle

 (optional) used as a fallback when a template exists but a title is not defined

```js
<DomHtml
  defaultTitle="My Site"
  titleTemplate="My Site - %s"
/>

// outputs:

<head>
  <title>My Site</title>
</head>
```

### onChangeClientState

(optional) callback that tracks DOM changes

```js
<DomHtml
  onChangeClientState={(newState, addedTags, removedTags) => console.log(newState, addedTags, removedTags)}
/>
```

Usefulness: This method can be set when it needs to be executed after `third JS` has been loaded.

```js
import React, { Component } from 'react';
import DomHtml from 'dom-casque';
import AMapCom from './AMapCom';

export default class AmapDemo extends Component {
  constructor(props){
    super(props);
    this.state = {
      loadAmap: false
    };
  }

  onDomChange = (newState, addedTags = {}, removedTags) => {
    // console.log(newState, addedTags, removedTags);
    const me = this;
    const amapScript = (addedTags.scriptTags || []).find(s => s.id === 'amapScripts');
    if (amapScript){
      amapScript.onload = amapScript.onreadystatechange = function(){
        if (amapScript.ready) {
          return false;
        }
        if (!amapScript.readyState || amapScript.readyState === 'loaded' || amapScript.readyState === 'complete') {
          amapScript.ready = true;
          me.setState({
            loadAmap: true
          });
        }
      };
    }

  render() {
    return (
      <div style={{ width: '100%', height: '100%' }}>
        <DomHtml onChangeClientState={this.onDomChange}>
          <meta charSet="utf-8" />
          <title>My Title</title>
          <script id="amapScripts" type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.3&key=your_sdk_key" />
        </DomHtml>
        {
          this.state.loadAmap && <AMapCom />
        }
      </div>
    );
  }
}
```

### as DomHtml children

#### html

html attributes

```js
<DomHtml>
  <html lang="en" amp />
</DomHtml>
```

#### body

body attributes

```js
<DomHtml>
  <body className="root" />
</DomHtml>
```

#### title

title attributes and value

```js
<DomHtml>
  <title itemProp="name" lang="en">My Plain Title or {`dynamic`} title</title>
</DomHtml>
```

#### base

base element

```js
<DomHtml>
  <base target="_blank" href="http://mysite.com/" />
</DomHtml>
```

#### meta

meta elements

```js
<DomHtml>
  <meta name="description" content="DomHtml application" />
  <meta property="og:type" content="article" />
</DomHtml>
```

#### link

multiple link elements

```js
<DomHtml>
  <link rel="canonical" href="http://mysite.com/example" />
  <link rel="apple-touch-icon" href="http://mysite.com/img/apple-touch-icon-57x57.png" />
  <link rel="apple-touch-icon" sizes="72x72" href="http://mysite.com/img/apple-touch-icon-72x72.png" />
  {locales.map((locale) => {
    <link rel="alternate" href="http://example.com/{locale}" hrefLang={locale} key={locale}/>
  })}
</DomHtml>
```

#### script

multiple script elements

```js
<DomHtml>
  <script src="http://include.com/pathtojs.js" type="text/javascript" />
  {/* inline script elements */}
  <script type="application/ld+json">
   {`
      {
        "@context": "http://schema.org"
      }
  `}
  </script>
</DomHtml>
```

#### noscript

noscript elements

```js
<DomHtml>
  <noscript>
  {`
    <link rel="stylesheet" type="text/css" href="foo.css" />
  `}
  </noscript>
</DomHtml>
```

#### style

noscript elements

```js
<DomHtml>
  <style type="text/css">
  {`
    body {
        background-color: blue;
    }

    p {
        font-size: 12px;
    }
  `}
  </style>
</DomHtml>
```

## changelog

[changelog](changelog.md)

## thanks

Base on [react-helmet](https://www.npmjs.com/package/react-helmet)

other component：

* [helmet](https://www.npmjs.com/package/helmet)
* [react-helmet-async](https://www.npmjs.com/package/react-helmet-async)
* [preact-helmet](https://www.npmjs.com/package/preact-helmet)
* [react-safety-helmet](https://www.npmjs.com/package/react-safety-helmet)
* [react-html-metadata](https://www.npmjs.com/package/react-html-metadata)

## Lecense

MIT

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