SpecReader Core API for creating spec readers and generating runtime metadata box API Reference
Categories

SpecReader

SpecReader processes spec files to generate optimized runtime metadata for defineComponent. It converts declarative spec objects into component specs with attribute mappings, type information, and default values.

Constructor

Creates a new SpecReader instance.

Syntax

new SpecReader(spec, options)

Parameters

NameTypeDescription
specObjectSpec object to process
optionsObjectConfiguration options

Options

NameTypeDefaultDescription
pluralbooleanfalseProcess as plural component
dialectstring’standard’HTML dialect: ‘standard’, ‘verbose’, or ‘classic’

Usage

Basic Usage

import { SpecReader } from '@semantic-ui/specs';
import buttonSpec from './button.spec.js';
const reader = new SpecReader(buttonSpec);

Plural Component

import { SpecReader } from '@semantic-ui/specs';
import buttonSpec from './button.spec.js';
const reader = new SpecReader(buttonSpec, { plural: true });

Custom Dialect

import { SpecReader } from '@semantic-ui/specs';
import buttonSpec from './button.spec.js';
const reader = new SpecReader(buttonSpec, { dialect: 'verbose' });

getWebComponentSpec()

Generates optimized component spec for defineComponent. Returns runtime metadata including attributes, property types, allowed values, and option mappings.

Syntax

reader.getWebComponentSpec(spec, options)

Parameters

NameTypeDefaultDescription
specObjectthis.specSpec to process
optionsObjectOverride options

Returns

PropertyTypeDescription
tagNamestringComponent tag name
attributesArrayList of attribute names
propertiesArrayList of property-only names
optionAttributesObjectMaps option values to attributes
propertyTypesObjectMaps properties to types
allowedValuesObjectMaps attributes to valid values
attributeClassesArrayAttributes that generate CSS classes
defaultValuesObjectDefault values for properties

Usage

Input spec:

button.spec.js
export default {
name: 'Button',
tagName: 'ui-button',
types: [
{
name: 'Emphasis',
attribute: 'emphasis',
options: [
{ name: 'Primary', value: 'primary' },
{ name: 'Secondary', value: 'secondary' },
],
},
],
variations: [
{
name: 'Size',
attribute: 'size',
options: [
{ name: 'Small', value: 'small' },
{ name: 'Large', value: 'large' },
],
},
],
};

Generate componentSpec:

build.js
import { SpecReader } from '@semantic-ui/specs';
import buttonSpec from './button.spec.js';
const reader = new SpecReader(buttonSpec);
const componentSpec = reader.getWebComponentSpec();

Output componentSpec:

button.component.js
{
tagName: 'ui-button',
attributes: ['emphasis', 'size'],
optionAttributes: {
'primary': 'emphasis',
'secondary': 'emphasis',
'small': 'size',
'large': 'size',
},
propertyTypes: {
'emphasis': 'string',
'size': 'string',
},
allowedValues: {
'emphasis': ['primary', 'secondary'],
'size': ['small', 'large'],
},
}
Previous
Specs
Next
Spec Helpers