Integrating Stencil Web Components in Angular Project

Integrating Stencil Web Components in Angular Project


Image description
Web Components is a suite of different technologies allowing you to create reusable custom user interface components.

In this article, we’ll explore how to create custom Web Components using Stencil.js and integrate them into Angular applications.

What is Stencil.js?
Stencil is an open-source compiler that generates standards-compliant web components. It builds highly performant, reusable components that can be used with any JavaScript framework or library. Created by the Ionic team, Stencil combines the best features of popular frameworks like Angular, React, and Vue, providing a simple and efficient way to build custom elements.

Advantages of Stencil Components

  • Stencil provides some syntactic sugar with TSX (JSX with TypeScript), which makes it much easier to build web components with cleaner, reduced code.
  • Stencil’s compiler performs static analysis on each component to customize the build. This results in a highly optimized runtime with minimal size
  • Stencil will automatically polyfill modern browser features and APIs for browsers that don’t support them.
  • Stencil uses APIs built directly within the browser instead of writing custom client-side JavaScript

# Creating a Stencil Project :

Step 1: Install Stencil CLI

First, we need to set up the stencil CLI globally. Go to your terminal and run the below command:

npm install -g @stencil/core
Enter fullscreen mode

Exit fullscreen mode

Step 2: Create a new Stencil Project

To create a new Stencil Project, Go to the desired folder location and run the below command:

npm init stencil
Enter fullscreen mode

Exit fullscreen mode

When you run the above command, it will be prompted to select the project type, Please select the component and provide a project name.

? Select a starter project.

Starters marked as [community] are developed by the Stencil Community,
rather than Ionic. For more information on the Stencil Community, please see
https://github.com/stencil-community » - Use arrow-keys. Return to submit.
>   component                Collection of web components that can be used anywhere
    app [community]          Minimal starter for building a Stencil app or website
    ionic-pwa [community]    Ionic PWA starter with tabs layout and routes
Enter fullscreen mode

Exit fullscreen mode

Step 3: Install Dependencies

Navigate to your stencil project location (cd your-stencil-project) and install the dependencies as shown below

npm install
Enter fullscreen mode

Exit fullscreen mode

Once the dependencies are installed, run the below command to make sure the Stencil project is running correctly.

 npm start
Enter fullscreen mode

Exit fullscreen mode

Go to this location and check your output http://localhost:33333

Image description

Creating a Web Components :

Now let’s create a custom Web Component. For this example, we’ll create a simple Button component.

Step 1: Generating a new Component

Go to your project location in the terminal and execute the below command to generate a new component:

npm run generate
Enter fullscreen mode

Exit fullscreen mode

You’ll be prompted to enter the name of your component. For this article, we name the button component as “new-button”. Enter the component name and press Enter.

> my-stencil-project@0.0.1 generate
> stencil generate

[38:18.7]  @stencil/core
[38:19.1]  v4.22.1
√ Component tag name (dash-case): ... new-button
√ Which additional files do you want to generate? » Stylesheet (.css), Spec Test  (.spec.tsx), E2E Test (.e2e.ts)

$ stencil generate new-button

The following files have been generated:
  - src\components\new-button\new-button.tsx
  - src\components\new-button\new-button.css
  - src\components\new-button\test\new-button.spec.tsx
  - src\components\new-button\test\new-button.e2e.ts
Enter fullscreen mode

Exit fullscreen mode

New components will be added to your project.

Image description

Step 2: Define the Component

Go to your generated component file location(src/components/new-button/new-button.tsx) and define your component as shown below:

import { Component, h,  Prop } from '@stencil/core';

@Component({
  tag: 'new-button',
  styleUrl: 'new-button.css',
  shadow: true,
})
export class NewButton {
  @Prop() text: string; 
   render() {
    return (
      
    );
  }
}
Enter fullscreen mode

Exit fullscreen mode

Note:

@Prop decorator lets you create parameters with access to component attributes.

Step 3: Add Styles

Add some basic styles to your component in the new-button.css file:

button {
  background-color: #0056b3;
  color: white;
  border: none;
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  border-radius: 4px;
}

button:hover {
  background-color:#1500ffde; 
}
Enter fullscreen mode

Exit fullscreen mode

Step 4: Build Your Component

Build your component by running:

npm run build
Enter fullscreen mode

Exit fullscreen mode

Step 5: Test your Component

You can test your component by adding the component name to your index.html


Enter fullscreen mode

Exit fullscreen mode

Here text is the label name of your button component.

Your index.html file looks like this:

Stencil Component Starter  

Leave a Reply

Your email address will not be published. Required fields are marked *