npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lytjs/dom

v6.9.6

Published

LytJS DOM utilities - Web Components integration and DOM enhancements

Readme

@lytjs/dom

DOM 工具包,提供 Web Components 集成、属性反射与自定义元素增强能力

安装

npm install @lytjs/dom

核心 API

类型转换器

内置类型转换器,用于在 JavaScript 属性值与 HTML attribute 字符串之间进行双向转换

import {
  StringConverter,
  NumberConverter,
  BooleanConverter,
  ObjectConverter,
  getConverterByType,
} from '@lytjs/dom';

// 字符串转换
StringConverter.toAttribute('hello'); // 'hello'
StringConverter.fromAttribute(null); // ''

// 数字转换
NumberConverter.toAttribute(42); // '42'
NumberConverter.fromAttribute('42'); // 42
NumberConverter.fromAttribute('abc'); // null

// 布尔转换
BooleanConverter.toAttribute(true); // ''
BooleanConverter.toAttribute(false); // null
BooleanConverter.fromAttribute(''); // true
BooleanConverter.fromAttribute(null); // false

// 对象转换(JSON 序列化)
ObjectConverter.toAttribute({ a: 1 }); // '{"a":1}'
ObjectConverter.fromAttribute('{"a":1}'); // { a: 1 }

// 根据类型自动获取转换器
const converter = getConverterByType('number'); // NumberConverter

AttributeReflector

属性反射管理器,处理 JavaScript 属性与 HTML attribute 之间的双向同步

import { AttributeReflector } from '@lytjs/dom';

const reflector = new AttributeReflector();

// 注册属性反射配置
reflector.register({
  prop: 'disabled',
  attr: 'disabled',
  converter: BooleanConverter,
});

// 批量注册
reflector.registerAll([
  { prop: 'title', converter: StringConverter },
  { prop: 'count', converter: NumberConverter },
]);

// 属性值同步到 attribute
reflector.reflectToAttribute(element, 'disabled', true);

// 从 attribute 同步到属性值
const result = reflector.reflectFromAttribute(element, 'disabled');
// result: { prop: 'disabled', value: true }

// 获取所有观察的 attribute 名称
reflector.getObservedAttributes(); // ['disabled', 'title', 'count']

createEnhancedElementClass

创建增强的 Web Component 基类,提供属性反射、变更观察和生命周期钩子

import { createEnhancedElementClass } from '@lytjs/dom';

const MyElement = createEnhancedElementClass({
  shadow: true,
  styles: ':host { display: block; }',
  properties: [
    { name: 'title', type: 'string', default: 'Hello' },
    { name: 'count', type: 'number', default: 0 },
  ],
});

// 继承并实现自定义逻辑
class MyCustomElement extends MyElement {
  protected onConnected(): void {
    console.log('Connected:', this.getProperty('title'));
  }

  protected onPropertyChanged(name: string, newValue: unknown): void {
    console.log(`${name} changed to`, newValue);
  }
}

customElements.define('my-element', MyCustomElement);

defineLytJSWebComponent

将 LytJS 组件注册为 Web Component(桥接功能开发中)

import { defineLytJSWebComponent } from '@lytjs/dom';

defineLytJSWebComponent(
  'my-lytjs-component',
  {
    component: MyLytJSComponent,
    propMapping: { title: 'label' },
    eventMapping: { change: 'on-change' },
  },
  {
    shadow: true,
    styles: ':host { display: block; }',
  },
);

工具函数

Web Components 环境检测与辅助函数

import {
  supportsWebComponents,
  whenDefined,
  isDefined,
  upgradeAll,
  camelToKebab,
  kebabToCamel,
} from '@lytjs/dom';

// 检测浏览器支持
if (supportsWebComponents()) {
  console.log('Web Components 可用');
}

// 等待自定义元素定义完成
await whenDefined('my-element');

// 检查是否已定义
isDefined('my-element'); // true

// 升级所有未升级的自定义元素
upgradeAll(document.body);

// 命名转换
camelToKebab('myComponent'); // 'my-component'
kebabToCamel('my-component'); // 'myComponent'

类型定义

import type {
  WCPropertyDefinition,
  WebComponentOptions,
  AttributeReflectionConfig,
  LytJSBridgeOptions,
} from '@lytjs/dom';

WCPropertyDefinition

Web Component 属性定义

interface WCPropertyDefinition {
  name: string;
  type?: 'string' | 'number' | 'boolean' | 'object' | 'array';
  default?: unknown;
  reflect?: boolean;
  onChange?: (newValue: unknown, oldValue: unknown) => void;
}

WebComponentOptions

Web Component 配置选项

interface WebComponentOptions {
  properties?: WCPropertyDefinition[];
  shadow?: boolean | ShadowRootInit;
  styles?: string;
  observedAttributes?: string[];
  extends?: string;
}

AttributeReflectionConfig

属性反射配置

interface AttributeReflectionConfig {
  prop: string;
  attr?: string;
  converter?: {
    toAttribute?: (value: unknown) => string | null;
    fromAttribute?: (value: string | null) => unknown;
  };
}

LytJSBridgeOptions

LytJS 组件与 Web Component 桥接选项

interface LytJSBridgeOptions {
  component: unknown;
  propMapping?: Record<string, string>;
  eventMapping?: Record<string, string>;
  slotMapping?: Record<string, string>;
}

相关包

依赖版本