Bulmil is an agnostic UI library based on Web Components, made with Bulma.io & Stencil.js.
Bulmil was created as a proof of concept to introduce an easy way to consume common reusable web components for use with various modern application frameworks (Angular, Vue, React, Ember) or simply with pure Javascript.
⚠️ Currently in Alpha, beta will be available once this issue is resolved.
Why Stencil?
Stencil is a compiler for building fast web apps using Web Components.
Stencil combines the best concepts of the most popular frontend frameworks into a compile-time rather than run-time tool. Stencil takes TypeScript, JSX, a tiny virtual DOM layer, efficient one-way data binding, an asynchronous rendering pipeline (similar to React Fiber), and lazy-loading out of the box, and generates 100% standards-based Web Components that run in any browser supporting the Custom Elements v1 spec.
Stencil components are just Web Components, so they work in any major framework or with no framework at all.
Getting Started
# Using npm npm i bulmil # Using yarn yarn add bulmil
Framework Integration
Stencil's primary goal is to remove the need for components to be written using a specific framework's API. It accomplishes this by using standardized web platform APIs that work across all modern browsers. Using the low-level component model that is provided by the browser (which all frameworks are built on) allows Stencil components to work inside of a framework or without one.
Stencil's integration with different frameworks is currently a work in progress. As Stencil matures, the goal is to make it easy to write standard web components which will compile to various output targets. This allows developers to stay aligned with the latest web standards while using a common API. The generated components will also be more future-proof as frameworks continue to change.
The following list contains the framework integrations that have been started. All of them are not yet completed.
Components without a Framework
Integrating a component built with Stencil to a project without a JavaScript framework is straight forward. If you're using a simple HTML page, you can add your component via a script tag. For example, if we published a component to npm, we could load the component through unpkg like this:
Button
Alternatively, if you wanted to take advantage of ES Modules, you could include the components using an import statement. Note that in this scenario applyPolyfills
is needed if you are targeting Edge or IE11.
Button
React
With an application built using the create-react-app
script the easiest way to include the component library is to call defineCustomElements(window)
from the index.js
file.
Note that in this scenario applyPolyfills
is needed if you are targeting Edge or IE11.
;;;;; ; ReactDOM.render, document.getElementById'root';registerServiceWorker; applyPolyfills.then;
Following the steps above will enable your web components to be used in React, however there are some additional complexities that must also be considered. https://custom-elements-everywhere.com/ describes them well.
Vue
In order to use the custom element library within the Vue app, the application must be modified to define the custom elements and to inform the Vue compiler which elements to ignore during compilation. This can all be done within the main.js
file.
Assuming you’ve run npm install --save bulmil
beforehand, and that bulmil
is the name of our made up Web Components that we have published to npm, you import the components into the 'main.js' file by
- importing the node module
- telling Vue to ignore the custom element tags (see
https://vuejs.org/v2/api/#ignoredElements
) - binding the Stenciljs component code to the window object
;; ; Vue.config.productionTip = false; // Tell Vue to ignore all components defined in the bulmil package.//T he regex assumes all components names are prefixed with 'bm-'Vue.config.ignoredElements = ; // Bind the custom elements to the window objectapplyPolyfills.then; new Vue.$mount'#app';
Using Nuxt
Create a plugin, (e.g bulmil.ts):
; ; Vue.config.productionTip = false;Vue.config.ignoredElements = ; // Bind the custom elements to the window objectapplyPolyfills.then;
// nuxt.config.ts
The components should then be available in any of the Vue components
render
Vue provides several different ways to install and use the framework in an application. The above technique for integrating a Stencil custom element library has been tested on a Vue application that was created using the vue-cli
with ES2015 and WebPack as primary options. A similar technique should work if the application was generated using other options.
Angular
Using a Stencil built web component collection within an Angular CLI project is a two-step process. We need to:
- Include the
CUSTOM_ELEMENTS_SCHEMA
in the modules that use the components. - Call
defineCustomElements(window)
frommain.ts
(or some other appropriate place).
Including the Custom Elements Schema
Including the CUSTOM_ELEMENTS_SCHEMA
in the module allows the use of the web components in the HTML markup without the compiler producing errors this code should be added into the AppModule
and in every other modules that use your custom elements.
Here is an example of adding it to AppModule
:
;;; ;
The CUSTOM_ELEMENTS_SCHEMA
needs to be included in any module that uses custom elements.
Calling defineCustomElements
A component collection built with Stencil includes a main function that is used to load the components in the collection. That function is called defineCustomElements()
and it needs to be called once during the bootstrapping of your application. One convenient place to do this is in main.ts
as such:
;; ;; // Note: loader import location set using "esmLoaderPath" within the output target confg; if environment.production platformBrowserDynamic .bootstrapModuleAppModule .catchconsole.logerr;defineCustomElementswindow;
Edge and IE11 polyfills
If you want your custom elements to be able to work on older browser, you should add the applyPolyfills()
that surrond the defineCustomElements()
function.
;...applyPolyfills.then
Accessing components using ViewChild and ViewChildren
Once included, components could be referenced in your code using ViewChild
and ViewChildren
as in the following example:
; ;
Ember
Working with Stencil components in Ember is really easy thanks to the ember-cli-stencil
addon. It handles:
- Importing the required files into your
vendor.js
- Copying the component definitions into your
assets
directory - Optionally generating a wrapper component for improved compatibility with older Ember versions
Start off by installing the Ember addon
ember install ember-cli-stencil
Now, when you build your application, Stencil collections in your dependencies will automatically be discovered and pulled into your application. You can just start using the custom elements in your hbs
files with no further work needed. For more information, check out the ember-cli-stencil
documentation.
Development
- Clone this repository
- Install dependencies using
yarn install
ornpm install
- Start development server using
yarn storybook
ornpm run storybook