import { html, nothing, type TemplateResult } from 'lit'; import { formatUsageFromDefaults, type PlaygroundControl, type PlaygroundDefinition, type PlaygroundPreviewHandlers, type PlaygroundProps, whenBoolean, whenString, } from '../types.js'; const ATTRIBUTE_NAMES: Record = { scrollContainer: 'scroll-container', deferredContentClass: 'deferred-content-class', nuiType: 'nui-type', }; export const NUI_DEFERRED_CONTENT_DEFAULTS: PlaygroundProps = { scrollContainer: '.deferred-content-viewport', deferredContentClass: '', loaded: false, unstyled: false, nuiType: '', }; const CONTROLS: PlaygroundControl[] = [ { key: 'scrollContainer', label: 'scroll-container', type: 'text', section: 'Content', placeholder: '.deferred-content-viewport', }, { key: 'deferredContentClass', label: 'deferred-content-class', type: 'text', section: 'Content', }, { key: 'nuiType', label: 'nui-type', type: 'text', section: 'Appearance' }, { key: 'loaded', label: 'loaded (preview state)', type: 'boolean', section: 'State', }, { key: 'unstyled', label: 'unstyled', type: 'boolean', section: 'Modifiers', }, ]; function handleLoad(_event: Event, handlers?: PlaygroundPreviewHandlers): void { if (!handlers) { return; } handlers.onPropChange('loaded', true); } function renderPreview( props: PlaygroundProps, handlers?: PlaygroundPreviewHandlers, ): TemplateResult { const loaded = Boolean(props.loaded); return html` Status: ${loaded ? 'loaded' : 'waiting for scroll'} Scroll this panel down to trigger deferred loading. Content is injected on @load so images and heavy markup are not created until visible. handleLoad(event, handlers)} > ${ loaded ? html` This card was rendered only after nui-deferred-content entered the viewport. ` : nothing } `; } export const nuiDeferredContentPlayground: PlaygroundDefinition = { tag: 'nui-deferred-content', label: 'Deferred content', description: 'Defers slotted content until the element scrolls into view; emits load once.', defaults: NUI_DEFERRED_CONTENT_DEFAULTS, controls: CONTROLS, renderPreview, getPreviewClass: () => 'is-fluid', formatUsage: (props) => formatUsageFromDefaults( 'nui-deferred-content', props, NUI_DEFERRED_CONTENT_DEFAULTS, ATTRIBUTE_NAMES, ), };
Status: ${loaded ? 'loaded' : 'waiting for scroll'}
Scroll this panel down to trigger deferred loading.
Content is injected on @load so images and heavy markup are not created until visible.
@load
nui-deferred-content