Skip to main content

API Reference

Complete reference for SmarterElements JavaScript API with iframe isolation, modal system, and automatic resizing.

CDN Version Management

unpkg.com Integration

SmarterElements is available via unpkg.com CDN with flexible versioning options:

Version Formats

FormatExampleDescriptionUse Case
@latest@smarterservices/smarter-elements@latestAlways gets the newest versionDevelopment, testing
@x.y.z@smarterservices/smarter-elements@1.2.3Specific version lockProduction (recommended)
@^x.y.z@smarterservices/smarter-elements@^1.2.0Compatible minor/patch updatesFlexible production
@~x.y.z@smarterservices/smarter-elements@~1.2.0Patch updates onlyConservative updates

Version Selection Strategy

<!-- Development: Always get latest features -->
<script src="https://unpkg.com/@smarterservices/smarter-elements@latest/dist/browser.js"></script>

<!-- Production: Lock to tested version -->
<script src="https://unpkg.com/@smarterservices/smarter-elements@1.0.24/dist/browser.js"></script>

<!-- Auto-updates: Patch fixes only -->
<script src="https://unpkg.com/@smarterservices/smarter-elements@~1.0.0/dist/browser.js"></script>

SmarterElements Class

Constructor

import { SmarterElements } from '@smarterservices/smarter-elements';
const elements = new SmarterElements(options);

Options

ParameterTypeDefaultDescription
baseUrlstring'/elements'Base URL for element endpoints
environmentstring'production'Environment: ‘development’, ‘production’, ‘stage’, ‘local’
defaultConstraintsobject{}Default size constraints for all elements

Example

const elements = new SmarterElements({
  baseUrl: '/elements',
  environment: 'development',
  defaultConstraints: {
    maxWidth: 1200,
    maxHeight: 800
  }
});

Element Creation

elements.create(type, options)

Creates a new element instance.

Parameters

ParameterTypeDescription
typestringElement type (e.g., ‘blockkit/renderer’)
optionsobjectElement configuration options

Options Object

PropertyTypeDescription
configobjectConfiguration data passed to element
constraintsobjectSize constraints (minWidth, maxWidth, minHeight, maxHeight)
autoResizebooleanEnable automatic resizing (default: true)
onReadyfunctionCallback when element is ready
onResizefunctionCallback when element resizes
onErrorfunctionCallback for errors
onEventfunctionCallback for custom events

Example

const element = elements.create('blockkit/renderer', {
  config: {
    blocksJson: JSON.stringify([
      { type: "heading", level: 1, text: "Hello World" }
    ])
  },
  constraints: {
    minWidth: 300,
    maxWidth: 800,
    minHeight: 200
  },
  onReady: () => console.log('Element ready'),
  onResize: ({ height, width, elementId }) => {
    console.log(`Resized: ${width}x${height}`);
  },
  onError: (error) => console.error('Error:', error)
});

Element Instance Methods

element.mount(selector)

Mounts element to DOM container. Parameters:
  • selector (string | HTMLElement) - Target container
// Mount to element with ID
element.mount('#container');

// Mount to first element with class  
element.mount('.element-container');

// Mount to DOM element reference
const container = document.getElementById('container');
element.mount(container);

element.openModal(options)

Opens element in modal overlay with automatic resizing and focus management. Parameters:
  • options (object) - Modal configuration options
Returns: Promise<void>
PropertyTypeDefaultDescription
widthstring | number'90%'Modal width (supports px, %, vw, etc.)
heightstring | numberautoModal height (auto-sizes by default)
maxWidthstring | number'800px'Maximum width constraint
maxHeightstring | number'90vh'Maximum height constraint
dismissOnDocumentClickbooleanfalseClose modal on backdrop click
escapeClosebooleantrueClose modal on ESC key press
zIndexnumber9999Modal z-index value
paddingstring'10px'Internal modal padding
// Basic modal
await element.openModal();

// Responsive modal with constraints
await element.openModal({
  width: '80%',
  maxWidth: '1000px',
  maxHeight: '90vh'
});

// Dismissible modal
await element.openModal({
  width: '600px',
  dismissOnDocumentClick: true,
  escapeClose: true
});

// Fixed size modal
await element.openModal({
  width: '800px',
  height: '600px'
});

element.closeModal()

Closes the modal if element is displayed as modal.
element.closeModal();

element.updateConfig(newConfig)

Updates element configuration. Parameters:
  • newConfig (object) - New configuration data
element.updateConfig({
  blocksJson: JSON.stringify([
    { type: "heading", level: 2, text: "Updated Content" }
  ])
});

element.updateConstraints(newConstraints)

Updates size constraints for mounted elements. Parameters:
  • newConstraints (object) - New constraint values
element.updateConstraints({
  maxWidth: 1000,
  maxHeight: 600
});

element.sendMessage(type, data)

Sends custom message to element. Parameters:
  • type (string) - Message type
  • data (any) - Message data
element.sendMessage('custom-event', {
  action: 'update',
  value: 'new-data'
});

element.destroy()

Destroys element and cleans up resources.
element.destroy();

React Integration

useSmarterElements(options)

React hook for managing SmarterElements. Parameters:
  • options (object) - Same as SmarterElements constructor options
Returns: SmarterElements instance
import { useSmarterElements } from '@smarterservices/smarter-elements';

function MyComponent() {
  const elements = useSmarterElements({
    baseUrl: '/elements',
    environment: 'development'
  });
  
  const openModal = async () => {
    const element = elements.create('blockkit/renderer', {
      config: { /* ... */ }
    });
    
    await element.openModal({
      width: '80%',
      maxWidth: '900px'
    });
  };
  
  return <button onClick={openModal}>Open Element</button>;
}

<SmarterElement> Component

React component for declarative element usage.
import { SmarterElement } from '@smarterservices/smarter-elements';

function MyComponent() {
  return (
    <SmarterElement
      type="blockkit/renderer"
      config={{
        blocksJson: JSON.stringify([
          { type: "text", text: "Hello from React!" }
        ])
      }}
      constraints={{
        maxWidth: 800,
        maxHeight: 600
      }}
      onReady={() => console.log('Element ready')}
      onResize={({ height, width }) => console.log('Resized:', { height, width })}
      style={{ border: '1px solid #ccc' }}
    />
  );
}

Size Constraints

Elements support flexible size constraints with CSS units:
const element = elements.create('blockkit/renderer', {
  constraints: {
    minWidth: 300,        // Pixels
    maxWidth: '80%',      // Percentage  
    minHeight: '20vh',    // Viewport height
    maxHeight: '600px'    // Pixels
  }
});

Supported Units

  • Pixels: 300 or '300px'
  • Percentages: '80%' (of parent container)
  • Viewport units: '50vw', '30vh'
  • Relative units: '20rem', '15em'

Event System

Element Events

const element = elements.create('blockkit/renderer', {
  onReady: () => console.log('Element ready'),
  onResize: ({ height, width, elementId }) => {
    console.log(`Element ${elementId} resized: ${width}x${height}`);
  },
  onError: (error) => console.error('Element error:', error),
  onEvent: (eventType, data) => {
    console.log('Custom event:', eventType, data);
  }
});

Standard Events

EventDataDescription
ready{ elementId }Element loaded and ready
resize{ width, height, elementId }Element dimensions changed
error{ error, elementId }Element encountered error
destroyed{ elementId }Element was destroyed

Utility Functions

debounce(func, wait, immediate?)

Delays function execution until after wait milliseconds have elapsed since the last time it was invoked. Parameters:
  • func (function) - The function to debounce
  • wait (number) - The number of milliseconds to delay
  • immediate (boolean, optional) - If true, trigger on leading edge
Returns: Debounced function with cancel() and flush() methods
import { debounce } from '@smarterservices/smarter-elements';

// Debounce resize handler
const debouncedResize = debounce(() => {
  element.recalculateSize();
}, 300);

window.addEventListener('resize', debouncedResize);

// Cancel pending execution
debouncedResize.cancel();

// Execute immediately
debouncedResize.flush();

throttle(func, wait, options?)

Limits function execution to at most once per wait milliseconds. Parameters:
  • func (function) - The function to throttle
  • wait (number) - The number of milliseconds to throttle to
  • options (object, optional) - { leading?: boolean, trailing?: boolean }
Returns: Throttled function with cancel() and flush() methods
import { throttle } from '@smarterservices/smarter-elements';

// Throttle scroll handler
const throttledScroll = throttle(() => {
  console.log('Scroll event');
}, 100);

window.addEventListener('scroll', throttledScroll);

CDN Usage

<script src="https://unpkg.com/@smarterservices/smarter-elements@latest/dist/browser.js"></script>
<script>
  const { debounce, throttle } = SmarterElements;
  
  const debouncedResize = debounce(() => {
    element.recalculateSize();
  }, 300);
  
  window.addEventListener('resize', debouncedResize);
</script>

TypeScript Support

Full TypeScript definitions included:
import { 
  SmarterElements, 
  SmarterElementInstance,
  ModalOptions,
  SmarterElementConstraints,
  debounce,
  throttle,
  DebouncedFunction
} from '@smarterservices/smarter-elements';

const elements = new SmarterElements({ baseUrl: '/elements' });

const element: SmarterElementInstance = elements.create('blockkit/renderer', {
  config: {
    blocksJson: string
  },
  constraints: {
    maxWidth: 800,
    maxHeight: 600
  } as SmarterElementConstraints
});

// Debounced function with type safety
const debouncedResize: DebouncedFunction<() => void> = debounce(() => {
  element.recalculateSize();
}, 300);

const modalOptions: ModalOptions = {
  width: '80%',
  maxWidth: '1000px',
  dismissOnDocumentClick: true
};

await element.openModal(modalOptions);

For more examples and use cases, see the Examples section.