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
| Format | Example | Description | Use Case |
|---|---|---|---|
@latest | @smarterservices/smarter-elements@latest | Always gets the newest version | Development, testing |
@x.y.z | @smarterservices/smarter-elements@1.2.3 | Specific version lock | Production (recommended) |
@^x.y.z | @smarterservices/smarter-elements@^1.2.0 | Compatible minor/patch updates | Flexible production |
@~x.y.z | @smarterservices/smarter-elements@~1.2.0 | Patch updates only | Conservative updates |
Version Selection Strategy
SmarterElements Class
Constructor
Options
| Parameter | Type | Default | Description |
|---|---|---|---|
baseUrl | string | '/elements' | Base URL for element endpoints |
environment | string | 'production' | Environment: ‘development’, ‘production’, ‘stage’, ‘local’ |
defaultConstraints | object | {} | Default size constraints for all elements |
Example
Element Creation
elements.create(type, options)
Creates a new element instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
type | string | Element type (e.g., ‘blockkit/renderer’) |
options | object | Element configuration options |
Options Object
| Property | Type | Description |
|---|---|---|
config | object | Configuration data passed to element |
constraints | object | Size constraints (minWidth, maxWidth, minHeight, maxHeight) |
autoResize | boolean | Enable automatic resizing (default: true) |
onReady | function | Callback when element is ready |
onResize | function | Callback when element resizes |
onError | function | Callback for errors |
onEvent | function | Callback for custom events |
Example
Element Instance Methods
element.mount(selector)
Mounts element to DOM container.
Parameters:
selector(string | HTMLElement) - Target container
element.openModal(options)
Opens element in modal overlay with automatic resizing and focus management.
Parameters:
options(object) - Modal configuration options
Promise<void>
Modal Options
| Property | Type | Default | Description |
|---|---|---|---|
width | string | number | '90%' | Modal width (supports px, %, vw, etc.) |
height | string | number | auto | Modal height (auto-sizes by default) |
maxWidth | string | number | '800px' | Maximum width constraint |
maxHeight | string | number | '90vh' | Maximum height constraint |
dismissOnDocumentClick | boolean | false | Close modal on backdrop click |
escapeClose | boolean | true | Close modal on ESC key press |
zIndex | number | 9999 | Modal z-index value |
padding | string | '10px' | Internal modal padding |
Modal Examples
element.closeModal()
Closes the modal if element is displayed as modal.
element.updateConfig(newConfig)
Updates element configuration.
Parameters:
newConfig(object) - New configuration data
element.updateConstraints(newConstraints)
Updates size constraints for mounted elements.
Parameters:
newConstraints(object) - New constraint values
element.sendMessage(type, data)
Sends custom message to element.
Parameters:
type(string) - Message typedata(any) - Message data
element.destroy()
Destroys element and cleans up resources.
React Integration
useSmarterElements(options)
React hook for managing SmarterElements.
Parameters:
options(object) - Same as SmarterElements constructor options
<SmarterElement> Component
React component for declarative element usage.
Size Constraints
Elements support flexible size constraints with CSS units:Supported Units
- Pixels:
300or'300px' - Percentages:
'80%'(of parent container) - Viewport units:
'50vw','30vh' - Relative units:
'20rem','15em'
Event System
Element Events
Standard Events
| Event | Data | Description |
|---|---|---|
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 debouncewait(number) - The number of milliseconds to delayimmediate(boolean, optional) - If true, trigger on leading edge
cancel() and flush() methods
throttle(func, wait, options?)
Limits function execution to at most once per wait milliseconds.
Parameters:
func(function) - The function to throttlewait(number) - The number of milliseconds to throttle tooptions(object, optional) -{ leading?: boolean, trailing?: boolean }
cancel() and flush() methods
CDN Usage
TypeScript Support
Full TypeScript definitions included:For more examples and use cases, see the Examples section.
