A framework independent component library based on Lit Element.
Ambacht Element is built on top of Lit Element for creating Web Components for your application. It should also work for libraries that support the usage of Web Components.
The processed Ambacht components can be implemented seperately and expects that dependencies are not bundled with the actual component. Instead it directly uses standard ESM imports to include the dependencies for your component.
This means that you don't need to include any dependencies to your package manager (NPM/Yarn), but it is still possible if you want to serve the dependencies from the same location as the created components. Keep in mind that you need to implement a resolver if you want to serve local dependencies; since modules cannot resolve from an absolute path.
An Ambacht element is created like a regular Lit Element. The actual styles, HTML and JS logic is defined within a single file. The workflow supports the usage of external stylesheets so you can use preprocessors like Node Sass or PostCSS. The generated stylesheets needs to be converted to a JS file so we can import it as module within the constructed Ambacht Element.
Note: Harbor supports the conversion of stylesheets as module exports. This workflow can process the required assets for your Ambacht Component. More information about implementing Ambacht with the Harbor workflow can be found within the Ambacht: README and Ambacht: Exporting assets as JS module for Web Components
Ambacht Element is created on top of the existing Lit Element library the following methods and default properties have been included to speed up the development process.
Ambacht Element includes some additional properties & dependencies for the initial component. These properties are required for the additional private & public methods.
Defines a custom name for the constructed component, the constructor name will be used as fallback.
A global flag that should block incomming Events within the constructed component. It should be used if you want to ensure Event stack from happening.
Enables the usage of inline SVG Sprite elements if the spriteSource
is defined for the initial component. A sprite element will be rendered if within the _renderImage
method when this property is defined.
Prevents the creation of a new Focus Trap instance for the defined component. The Focus Trap related helpers will do anything when this property is defined.
Stores the handler that was defined from this._throttle
method to ensure it is only called once.
Defines the throttle timeout in miliseconds for the constructed Ambacht component. It will default to 60 FPS in miliseconds.
Defines the keyCode values that should be ignored during a Keyboard Event. It should ignore the functional keyCodes like shift
return
tab
etc.
Defines the keyCode values that should exit out of the current function scope from the component. This can be used to reset the component state like collapsing the element after the escape
key is pressed.
Defines the list of accepted image extensions that is used within the _renderImage
_testImageSource
& _testImage
methods.
Should match with the global scope of the JS context. This should be the Window object. The Ambacht
global will be implemented within the given root
context that is also used to mark the currentElement
.
The default component reference that should be assigned to the top parent element within the render
method.
Implements the core Ambacht dependencies like Focus Trap for the defined component.
Ensures the currentElement
value is updated if the component has been (de)selected and that the Ambacht
global is updated afterwards.
Enables the defined Focus Trap within the current component.
Disables the defined Focus Trap within the current component.
Alias function that uses the default Console object from the Browser.
Subscribes a new Document Event listener to the globalEvents private property.
Removes the subscribed Document Event listener from the globalEvents private property.
These methods are Private and should only be used within the component instance. Private methods are prefixed with an underscore.
Ensures the given string is formatted as snake cased value to ensure the compatibility for HTML attributes.
Implements a click Event handler for the current component. It will mark the component as currentElement
within the Ambacht global.
Implements a keydown handler for the created Ambacht component to remove the component as currentElement
.
Catches the focus Event within the component to mark it as currentElement
.
Check if the defined Function handler has been defined to the documentEvents
property.
Helper function to initiate the passed handler. It can initiate the function directly or an Event will be dispatched within the given context.
Marks the defined context as currentElement
this function is also used within the _global
Event functions like _globalKeyDownFunction
.
Wrapper function that will call LitElement.requestUpdate
method within a Catch block. A function scope can be included instead to preform tasks before the requestUpdate is called.
// Updates the title property and call the requestUpdate method directly.
this._update('title', 'Bar');
// Will call the requestUpdate method for the title after the defined handler.
this._update('title', () => {
...
this.title = 'Bar'
...
});
Ensure any whitespace characters are removed from the defined string.
this._strip('Foo \n Bar'); // Expected value: FooBar
Implements support for Function handlers defined from the Window scope or Function Handlers within the component scope.
this._fn('onRender'); // Calls the function from the global scope (this should be the Window Object)
this._fn(this.OnRender); // Calls the function from the Component scope.
Renders the supported images defined within AmbachtElement.supportedImageExtensions
as inline or external element. An inline SVG element will be used if this.spriteSource
is defined for the initial component.
// Will render the given image within an img tag.
this._renderImage('https://img.icons8.com/ios-glyphs/2x/search.png');
// Will render the given image within an img tag since the
// spriteSource has not been defined for this component.
this._renderImage('example.svg', 'svg-icon'); // -> example.svg
// Will render the given image as a SVG inline sprite since the
// spriteSource has been defined for this component.
this.spriteSource = 'sprite.svg';
this._renderImage('example', 'svg-icon'); // -> sprite.svg#example
Test if the defined source
parameter matches with the expected supportedImageExtensions
values. This is internally used within the _renderImage
method to defined the correct image element.
Tests if the defined source is a valid image extension that exists within the defined supportedImageExtensions
property. The defined path parameter will also be escaped to ensure no white space is present for the source parameter.
Throttles the given Function handler to ensure it is only called once and respects the default refresh rate of the component. The actual delay can be adjusted by defined the throttleTimeout
property to the component. By default it will throttle at 60 FPS.
Checks if the given Element exists within the Shadow Dom and Regular Dom within the current Component.
Should return the main context for the current component this will be the actual Shadow Dom if there is no parent reference defined from the current component.