Navigate back to the homepage

Implementing iOS 14 Weather Widget As A Web Component

Dennis Morello
June 28th, 2020 · 5 min read

At WWDC20, Apple announced iOS 14, the next major release of one of the most popular mobile OSes. Among the new features, they finally added the ability to put widgets on the home screen:

Widgets on the home screen on iOS 14
Source: Apple

In this article, we will port the beautiful weather widget of iOS 14 to the web, implementing it as an universal Web Component.

Source Code & Demo

You can find the source code for this project at this GitHub repository. A live demo is also available here!

Web Components

Web Components is a name that refers to a stack of different technologies that enable us to create reusable custom HTML elements:

1<login-button></login-button>

In particular, a Web Component is made up of 3 things:

  • Custom Elements – A set of JavaScript APIs that allow you to define custom elements and their behaviour, which can then be used as desired in your user interface.
  • Shadow DOM – A set of JavaScript APIs for attaching an encapsulated “shadow” DOM tree to an element — which is rendered separately from the main document DOM — and controlling associated functionality. In this way, you can keep an element’s features private, so they can be scripted and styled without the fear of collision with other parts of the document.
  • HTML Templates – The <template> and <slot> elements enable you to write markup templates that are not displayed in the rendered page. These can then be reused multiple times as the basis of a custom element’s structure.

For a deep dive into each of these terms, you can refer to the exhaustive MDN resources. The interesting thing to note is that they are supported by all major web browsers:

There are a bunch of tools for building Web Components without having to manually define a custom element, creating a shadow DOM root, attacching it to a DOM node and taking care of the interactions with other elements. Among others, we will here use Stencil.

Stencil

Stencil is a toolchain for building highly optimized and 100% standards based Web Components that run in every browser:

Stencil Website
Stencil Website

It is developed and maintained by the Ionic Framework team and combines the best concepts of the most popular frameworks into a simple build-time tool, such as:

  • Virtual DOM
  • Async rendering (inspired by React Fiber)
  • Reactive data-binding
  • TypeScript
  • JSX
  • Static Site Generation (SSG)

Getting Started

To start a new Stencil project, type the following command into a terminal (make sure to have a recent LTS version of NodeJS and npm v6 or higher):

1npm init stencil

Stencil can be used to create standalone components, or entire apps. After running init you will be provided with a prompt so that you can choose the type of project to start. In our case, let’s choose component:

1? Pick a starter › - Use arrow-keys. Return to submit.
2
3 ionic-pwa Everything you need to build fast, production ready PWAs
4 app Minimal starter for building a Stencil app or website
5❯ component Collection of web components that can be used anywhere

Next, we need to provide a name for the project, for example "ios-14-weather-widget". After that, a folder containing our project is created:

Stencil project structure
Stencil project structure

Stencil Project Structure

Our project contains a bunch of files and folders, including a configuration file for TypeScript (tsconfig.json) and one for the Stencil compiler (stencil.config.ts).

The src/ folder contains the source files for our project. The most important ones are:

  • src/index.html – The main HTML file
  • src/components/ – Contains our custom components (like any React project)

Anatomy Of A Stencil Component

Stencil components are created by adding a new file with a .tsx extension inside the src/components/ directory – the .tsx extension is required since Stencil components are built using JSX and TypeScript:

Stencil component
A Stencil component

As we can see, the my-component/ folder contains other files along the .tsx one. In particular, it contains a CSS file (my-component.css), an unit test file (my-component.spec.ts) and an end-to-end test file (my-component.e2e.ts).

We can delete the exising src/components/my-component/ directory because we will generate one for our widget later.

Creating A New Component

We have two ways to create a new component: we can either manually create a folder under the src/components/ directory with a .tsx file inside it, or we can use the generate NPM script specified in package.json.

We choose the second way, since it is quicker and standard. So, for the weather widget component, type this on the terminal and hit Enter:

1npm run generate weather-widget

Next, the generator script asks us which additional files we want to generate alongside the .tsx one:

1? Which additional files do you want to generate? ›
2Instructions:
3 ↑/↓: Highlight option
4 ←/→/[space]: Toggle selection
5 a: Toggle all
6 enter/return: Complete answer
7
8◉ Stylesheet (.css)
9◯ Spec Test (.spec.tsx)
10◯ E2E Test (.e2e.ts)

For the purpose of this article, we can choose only Stylesheet (.css), so the script generates a stylesheet file for our component. When we hit Enter, the final result is this:

Weather widget component
The weather-widget component

For a detailed explanation of what is going on here, please refer to the excellent Stencil documentation – in particular, the Components section covers everything we need to know.

The Weather Widget Component

We want to reproduce the original iOS 14 weather widget:

Original iOS 14 weather widget
The original iOS 14 weather widget – Courtesy: Apple

To do so, our component needs to show the following information:

  • The location name
  • The temperature
  • An icon representing the weather conditions
  • A textual description of the weather conditions
  • The minimum and the maximum temperature of the day

So, we add some attributes that the component will expose publicly – sort of props in the React world. Let’s do it by importing the Prop decorator from @stencil/core and by adding some class members decorated with it:

1import { Component, ComponentInterface, Host, Prop, h } from "@stencil/core";
2
3@Component({
4 tag: "weather-widget",
5 styleUrl: "weather-widget.css",
6 shadow: true
7})
8export class WeatherWidget implements ComponentInterface {
9 @Prop() location: string = "Milan";
10 @Prop() condition: string = "Mostly Sunny";
11 @Prop() tempHigh: number = 32;
12 @Prop() tempLow: number = 24;
13 @Prop() temperature: number = 28;
14
15 render() {
16 return (
17 <Host>
18 <slot></slot>
19 </Host>
20 );
21 }
22}

Note that we have initialized the props with hardcoded values – it is fine for now. Next, let’s edit the render() method in order to display the data:

1render() {
2 return (
3 <Host>
4 <div>
5 <h3 class="location">{this.location}</h3>
6 <h4 class="temperature">{this.temperature}°</h4>
7 </div>
8 <div>
9 <ion-icon class="weather icon" name="sunny"></ion-icon>
10 <p class="weather condition">{this.condition}</p>
11 <p class="weather temperatures">
12 <span class="temp-high">H:{this.tempHigh}°</span>
13 <span class="temp-low">L:{this.tempLow}°</span>
14 </p>
15 </div>
16 </Host>
17 );
18}

This is a simple markup that implements the structure of the widget. We have also given classes to elements so we can easily style them using CSS. The curious thing to note here is that on line 9 we are using the <ion-icon> element: as you guessed, this is not a standard W3C HTML element; instead, it is a Web Component coming from the Ionicons icon library.

Before we can have a preview of what we did, let’s edit the src/index.html to include what we need:

1<!DOCTYPE html>
2<html dir="ltr" lang="en">
3 <head>
4 <meta charset="utf-8" />
5 <meta
6 name="viewport"
7 content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0"
8 />
9 <title>iOS 14 Weather Widget</title>
10
11 <script type="module" src="/build/ios-14-weather-widget.esm.js"></script>
12 <script nomodule src="/build/ios-14-weather-widget.js"></script>
13
14 <!-- Ionicons -->
15 <script
16 type="module"
17 src="https://unpkg.com/ionicons@5.1.2/dist/ionicons/ionicons.esm.js"
18 ></script>
19 <script
20 nomodule=""
21 src="https://unpkg.com/ionicons@5.1.2/dist/ionicons/ionicons.js"
22 ></script>
23
24 <!-- normalize.css -->
25 <link
26 rel="stylesheet"
27 href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css"
28 />
29 </head>
30 <body>
31 <weather-widget></weather-widget>
32 </body>
33</html>

Let’s quickly review what’s changed:

  • On line 9 we just changed the page title.
  • On lines 14-22 we added the script for loading the Ionicons package.
  • On lines 24-28 we added Normalize.css, a CSS reset utility that makes browsers render all elements more consistently and in line with modern standards.
  • On line 31 we have replaced the predefined Web Component with the newly created <weather-widget>.

We can now have a preview of what we did! Type npm start in a terminal and hit Enter, then a browser window will automatically appear showing the index.html:

First weather widget preview
First weather-widget preview

Ok, this definetely does not look like the original iOS weather widget yet, but look at the page inspector inside the Developer Tools: the DOM does contain a <weather-widget> element that the browser is actually understanding and rendering! 🤩

Pro Tip: From the page inspector panel, try to add a location attribute to the <weather-widget> element with a value of your choice, then see how the text "Milan" is replaced by what you specified!

Making The Component Beautiful ✨

Now that we wrote down the structure of our component, let’s style it trying to get as close as we can to the iOS original widget.

Open the src/components/weather-widget/weather-widget.css file and replace its content with:

1:host {
2 display: flex;
3 flex-direction: column;
4 justify-content: space-between;
5 width: 256px;
6 height: 256px;
7 border-radius: 32px;
8 color: #ffffff;
9 background: linear-gradient(#3f8ab3, #6ab3d6);
10 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
11 Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
12 padding: 24px;
13 box-sizing: border-box;
14 user-select: none;
15 box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
16}
17
18.location {
19 margin: 0;
20 font-weight: 500;
21 font-size: 20px;
22}
23
24.temperature {
25 margin: 0;
26 font-weight: 200;
27 font-size: 64px;
28}
29
30.weather {
31 margin: 0;
32 font-weight: 400;
33 font-size: 18px;
34}
35
36.weather.icon {
37 color: #f8d24b;
38 font-size: 28px;
39 margin-bottom: 4px;
40}
41
42.weather.condition {
43 margin-bottom: 4px;
44}
45
46.weather.temperatures {
47 font-variant-numeric: tabular-nums;
48}
49
50.temp-high {
51 margin-right: 8px;
52}

The :host pseudo-class selects the DOM node to which the shadow DOM of our custom Web Component is attached. We are simply saying that it is a flex container of size 256x256 pixels, whose children flows in column and have space between them.

All the rules listed here have been inferred from the original Apple weather widget.

Once we save the file, the dev server automatically reloads the page, and this is the result:

Weather widget with style
weather-widget with style

Wow, it looks pretty close to the original one!

Tidying Things Up

For the sake of completeness, let’s add a background image to the page and center the widget.

To do so, we need to add a global style in the container page, so let’s create a src/global/ folder inside which we create a style.css file and an assets/ folder.

Then, open the src/global/style.css file and paste the following:

1body {
2 display: grid;
3 place-items: center;
4 height: 100vh;
5 grid-auto-rows: 100vh;
6 background-image: url("/assets/background.jpg");
7 background-repeat: no-repeat;
8 background-size: cover;
9}

Then, copy the background image to the src/global/assets/background.jpg file – I chose one of the original iOS 14 wallpapers.

Now that we did this, we need to tell the Stencil compiler to take those 2 files into account. To do so, open the stencil.config.ts and add the highlighted lines:

1import { Config } from "@stencil/core";
2
3export const config: Config = {
4 namespace: "ios-14-weather-widget",
5 globalStyle: "src/global/style.css",
6 taskQueue: "async",
7 outputTargets: [
8 {
9 type: "dist",
10 esmLoaderPath: "../loader"
11 },
12 {
13 type: "docs-readme"
14 },
15 {
16 type: "www",
17 copy: [
18 {
19 src: "global/assets/background.jpg",
20 dest: "assets/background.jpg"
21 }
22 ],
23 serviceWorker: null // disable service workers
24 }
25 ]
26};

Line 5 tells the compiler where our global stylesheet file is located, while lines 17-22 are needed in order to copy the background image to the final bundle.

The last thing to do is to update the src/index.html in order to add the compiled global stylesheet using the <link> tag inside the <head> element:

1<link rel="stylesheet" href="/build/ios-14-weather-widget.css" />

Finally, stop the running dev server and start it again, so the new configuration file is reloaded, and…

Final result
Final result

Whoa, it looks so beautiful 😍

Conclusions

What we built in this article is just a simple Web Component that receives some attributes and renders them.

In a future post, we will see how to read the real user location and how to automatically fetch the weather data.

Thanks for reading!


If you have thoughts or questions on this post, please mention me on Twitter!

More articles from Dennis Morello

React Awesome Reveal

Meet my new library for animating React components using Intersection Observer API and CSS Animations!

December 15th, 2019 · 2 min read

You Will Stop Using LocalStorage

Goodbye LocalStorage, welcome Key-Value Storage!

April 25th, 2019 · 2 min read
© 2019–2020 Dennis Morello
Link to $https://twitter.com/dennismorelloLink to $https://github.com/dennismorelloLink to $https://instagram.com/dennismrlLink to $https://www.linkedin.com/in/dennismorelloLink to $mailto:dennismorello@gmail.com