@molecule/app-inline-style-dom

Utility · ui · App (browser) · v1.2.0 · Apache-2.0

React-compatible inline style serialization and DOM updates

npm install @molecule/app-inline-style-dom

npm · Source on GitHub

How it works

@molecule/app-inline-style-dom is a utility package for the app (browser) side (ui).

import { bindInlineStyle, reactStyleText } from '@molecule/app-inline-style-dom'

const style = { marginTop: 8, opacity: 0.5, '--accentHue': 280 }
const attribute = reactStyleText(style)
const element = document.querySelector<HTMLElement>('[data-mol-id="preview"]')
if (element) {
  const binding = bindInlineStyle(element, style)
  binding.update({ marginTop: 0, opacity: 1 })
}

Reference

React-compatible inline style serialization and DOM updates for web UI kits.

Serialize React-shaped style objects for HTML attributes with reactStyleText. Use toStyleProps when a renderer takes CSS property names and string values. Use bindInlineStyle to update an element's styles without replacing its entire style attribute.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { bindInlineStyle, reactStyleText } from '@molecule/app-inline-style-dom'

const style = { marginTop: 8, opacity: 0.5, '--accentHue': 280 }
const attribute = reactStyleText(style)
const element = document.querySelector<HTMLElement>('[data-mol-id="preview"]')
if (element) {
  const binding = bindInlineStyle(element, style)
  binding.update({ marginTop: 0, opacity: 1 })
}

Type

utility

Installation

npm install @molecule/app-inline-style-dom

API

Interfaces

InlineStyleBinding

Inline styles applied to one element.

interface InlineStyleBinding {
  /** Apply the next style object and clear declarations that dropped out. */
  update: (style: unknown) => void
  /** Clear the binding's declarations from the element. */
  destroy: () => void
}

CoverImageStyle

The background declarations an element paints for its style.coverImage.

interface CoverImageStyle {
  /** The image, as a CSS `url()`; undefined for an empty URI, which paints no image. */
  backgroundImage: string | undefined
  /** The image covers the whole box. */
  backgroundSize: 'cover'
  /** The image is centred in the box. */
  backgroundPosition: 'center'
}

Types

CSSStyle

A React-shaped inline style object. Custom property names retain their spelling.

type CSSStyle = Record<string, string | number | boolean | null | undefined>

Functions

toCssPropertyName(name)

Converts a React property name to its CSS spelling.

function toCssPropertyName(name: string): string
  • name — A camelCase, vendor-prefixed or custom property name.

Returns: The CSS property name.

coverImageStyle(url)

The declarations an element's style.coverImage (BoxProps['style'] in @molecule/app-ui) paints on the web: the image behind the contents, centred and sized to cover the box.

function coverImageStyle(url: string | undefined): CoverImageStyle
  • url — The image URI, or undefined for none.

Returns: The background declarations, in the order the React kit writes them.

reactStyleText(style, space)

Serializes a React-shaped style object as React DOM's style attribute value. The renderer escapes the value when it writes HTML.

function reactStyleText(style: unknown, space?: '' | ' '): string | undefined
  • style — The style object, or an absent value.
  • space — An optional space after separators for existing bindings that use spaced CSS.

Returns: The CSS declaration text, or undefined when there are no declarations.

toStyleProps(style)

Normalizes a React-shaped style object for renderers that accept a CSS property map.

function toStyleProps(style: unknown): Record<string, string> | undefined
  • style — The style object, or an absent value.

Returns: CSS property names and string values, or undefined for an absent object.

bindInlineStyle(node, style)

Binds a React-shaped style object to an element. Changes reach the CSSOM in object key order. The binding clears declarations it applied and preserves styles from other writers.

function bindInlineStyle(node: ElementCSSInlineStyle, style: unknown): InlineStyleBinding
  • node — An HTML or SVG element with inline styles.
  • style — The initial style object, or an absent value.

Returns: The binding's update and cleanup hooks.