1.0.0 • Published 13 days ago

@rabiepenpm/distinctio-nostrum-nulla v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

@rabiepenpm/distinctio-nostrum-nulla

Generate/register a custom element from a preact component. As of 3.0.0, this library implements the Custom Elements v1 spec. Previous versions (< 3.0.0) implemented the v0 proposal, which was only implemented in Chrome and is abandoned.

Usage

Import register and call it with your component, a tag name*, and a list of attribute names you want to observe:

import register from '@rabiepenpm/distinctio-nostrum-nulla';

const Greeting = ({ name = 'World' }) => (
  <p>Hello, {name}!</p>
);

register(Greeting, 'x-greeting', ['name']);

* Note: as per the Custom Elements specification, the tag name must contain a hyphen.

Use the new tag name in HTML, attribute keys and values will be passed in as props:

<x-greeting name="Billy Jo"></x-greeting>

Output:

<p>Hello, Billy Jo!</p>

Prop Names and Automatic Prop Names

The Custom Elements V1 specification requires explictly stating the names of any attributes you want to observe. From your Preact component perspective, props could be an object with any keys at runtime, so it's not always clear which props should be accepted as attributes.

If you omit the third parameter to register(), the list of attributes to observe can be specified using a static observedAttributes property on your Component. This also works for the Custom Element's name, which can be specified using a tagName static property:

import register from '@rabiepenpm/distinctio-nostrum-nulla';

// <x-greeting name="Bo"></x-greeting>
class Greeting extends Component {
  // Register as <x-greeting>:
  static tagName = 'x-greeting';

  // Track these attributes:
  static observedAttributes = ['name'];

  render({ name }) {
    return <p>Hello, {name}!</p>;
  }
}
register(Greeting);

If no observedAttributes are specified, they will be inferred from the keys of propTypes if present on the Component:

// Other option: use PropTypes:
function FullName(props) {
  return <span>{props.first} {props.last}</span>
}
FullName.propTypes = {
  first: Object,   // you can use PropTypes, or this
  last: Object     // trick to define untyped props.
};
register(FullName, 'full-name');

Related

preact-shadow-dom

testingrecursivedependency managerqsequalirqArrayBuffer#sliceomitchinesesafeslicehttpsencryption256compilervaluesBigInt64Arrayreadablestreamreadablevalidutilitycomputed-typesMicrosoftoffsettacitreal-timewebfindupfast-copytakefetchprefixnativefoldergetPrototypeOfArray.prototype.flatflattenObject.valuesECMAScript 2017waitredux-toolkitstringifydircoercibleeslint-pluginargumentserialization@@toStringTagstatelessstarternpmentriesconsttypeswgetReactiveXeast-asian-widthtypeES2023traverseduplexpositivebabelpyyamltslibauthenticationfunctionpurebyteOffsetdeepWeakSetdeepcopyobjconcurrencyStreamscachetypedarraymonorepoimportemitdefaultsequenceidlemkdirworkergetteransiiterationYAMLastinterruptspackagesnodejsvalidatorcurriedcomparesetterUint8ArraydateresolveuploadingeventDispatcheranimationObject.definePropertyRFC-6455HyBiInt8Arrayjson-schema-validationoptimistefficientparentxdg-openkeyclassnamespropdayjsECMAScript 2023ECMAScript 3arttrimEndlaunchtypanionjapaneseclonereadURLutil.inspectgetoptsanitizenegative zerofastcss lesstyped arrayassignnumbereventEmittertoStringTagexeeditorpackageapisigintmochawatchingscheme-validationyamldomsettingseslintweaksetdom-testing-libraryfile systemsyntaxes-shim APIsymbolsloggingdeep-cloneimmutablecolorsStreamescapeprivate datawalkingbreakESnextObject.fromEntriesdataViewbyteLengthlistenersnamesopensclassnameairbnblruhashfind-upwatchFilecommandpipespawnlesscssextensionsomecallbindReflect.getPrototypeOfless mixinsSetdebugminimaleventsES2020zodregex_.extendflagsWebSocketuuidauthtappoint-freedeletebundlingargvString.prototype.matchAlles2017toArraybootstrap csspromiseframeworkhigher-orderECMAScript 7es2018pretty[[Prototype]]lengthlintstablerapidoperating-systemtextfsserializeappjsonschemaArray.prototype.containsAsyncIterator__proto__corelazydeep-copyes-shimsiteratedataprunestylescliarktypeaccessibilityES7reducercore-jsruntimemodulecodeswhichECMAScript 5stringdiffunicodeES5parentsreducehooksguidtouchtypeofparseio-tsredactxtermurlsposetermcss nestingbootstrap lesssetpostcssdragiteratorwordbreakstylingcallboundcall-boundrobustmruArrayBufferviewtrimStartchaicryptojsonpathlimitquotegdprECMAScript 2018Array.prototype.findLastIndexmomentsuperagentformatcolornopecompile lessautoprefixerrgbinstallerreact posedescriptorsiefixed-widthcolumnindicatorthreeratetapeasyncmulti-packagerm -frreact animationopenhasOwnmakexhrvarcall-bindcssqueueMicrotaskes2015Object.assignprocesswaapibundlerprotobufstructuredCloneopenerdotenvessignalrmdirtoolsfunctionsnegativerandomenvironmentES2016tddUint32ArrayCSSStyleDeclarationconnectrmsuperstructfast-deep-copyloggerECMAScript 2019a11yString.prototype.trimstringifiermime-dbvariables in csspersistentdefinestyleguidesymbolfiltercjknodeUint8ClampedArrayObservablebindpasswordArrayBuffer.prototype.sliceprotocol-bufferspackage managerregular expressionsES2021jasminearraybuffercmdchromeenvassertioncommanderoutputfseventscurlflatMapframerarrayswriteECMAScript 2016typeerrortrimRightfastcopyutilitiesPushjson-schema-validatorsortexpressECMAScript 2021fpajvvaluecopyawaitvisualTypedArraycode pointsObject.ispicomatchsharedttydeepclonetypescriptES2022flagstreamspolyfillcheckes5error-handlingreact-testing-librarytypesafefigletisConcatSpreadabletestECMAScript 2020rangeerrorconfigurable.envMapObject.keysmimees8harmonyArray.prototype.flatMapRegExp#flagsbluebirdworkspace:*JSON-Schemamobilecreatepopmotionbcryptcommand-linetelephonegraphqlgradients css3charactersl10nFunction.prototype.nameschemametadataArray.prototype.findLastform-validationstylesheetWebSocketsRxtc39performantmatchshellmkdirsapollohookformObservablesformconsumewidth6to5variablesjavascriptArray.prototype.includesimmernested csspropertieswatchboundgetOwnPropertyDescriptorcolourtoSortedfastifywalkbddyupmixinsBigUint64ArrayexpressionregularphonegesturesbusycontainsqueryhasmoveconfiggenericsESmimetypesincludesrfc4122stream3dslotsigtermspeedjsdom
1.0.0

13 days ago