Creating Color Themes With Custom Properties, HSL, and a Lit...

Creating Color Themes With Custom Properties, HSL, and a Lit…

Before the advent of CSS custom properties (we might call them “variables” in this article as that’s the spirit of them), implementing multiple color schemes on the same website usually meant writing separate stylesheets. Definitely not the most maintainable thing in the world. Nowadays, though, we can define variables in a single stylesheet and let CSS do the magic.

Even if you aren’t offering something like user-generated or user-chosen color themes, you might still use the concept of theming on your website. For example, it is fairly common to use different colors themes across different areas of the site.

We’re going to build out an example like this:

Same layout, different colors.

In this example, all that changes between sections is the color hue; the variations in lightness are always the same. Here’s an example of a simplified color palette for a specific hue:

A palette of multiple hues might look something like this:

This would take effort to do with RGB color value, but in HSL only one value changes.

Enter custom properties

Custom properties have been around for a while and are widely supported. Polyfills and other solutions for IE 11 are also available.

The syntax is very similar to traditional CSS. Here is an overview of the basic usage:

It’s common to see variables defined on the :root pseudo-element, which is always <html> in HTML, but with higher specificity. That said, variables can be defined on any element which is useful for scoping specific variables to specific elements. For example, here are variables defined on data attributes:

Adding calc() to the mix

Variables don’t have to be fixed values. We can leverage the power of the calc() function to automatically calculate values for us while adhering to a uniform pattern:

Since CSS doesn’t support loops, a preprocessor would be handy to generate a part of the code. But remember: CSS variables are not the same as Sass variables.

Implementing CSS variables

What we’re basically trying to do is change the color of the same component on different sections of the same page. Like this:

We have three sections in tabs with their own IDs: #food, #lifestyle, and #travel. Each section corresponds to a different hue. The  data-theme-attribute on the div.wrapper element defines which hue is currently in use.

When #travel is the active tab, we’re using the --first-hue variable, which has a value of 180°. That is what gets used as the --hue value on the section, resulting in a teal color:

<div class="wrapper" data-theme="travel">
.wrapper[data-theme="travel"] {
  --hue: var(--first-hue);  /* = 180° = teal */

Clicking any of the tabs updates the data-theme attribute to the ID of the section, while removing the hash (#) from it. This takes a smidge of JavaScript. That’s one of the (many) nice things about CSS: they can be accessed and manipulated with JavaScript. This is a far cry from preprocessor variables, which compile into values at the build stage and are no longer accessible in the DOM.

<li><a href="">Food</a></li>
const wrapper = document.querySelector('.wrapper');
document.querySelector("nav").addEventListener('click', e => {
  // Get theme name from URL and ditch the hash
  wrapper.dataset.theme ='href').substr(1);

Progressive enhancement

When we use JavaScript, we should be mindful of scenarios where a user may have disabled it. Otherwise, our scripts — and our UI by extension — are inaccessible. This snippet ensures that the site content is still accessible, even in those situations:

document.querySelectorAll('section').forEach((section, i) => {
  if (i) { // hide all but the first section = 'none';

This merely allows the tabs to scroll up the page to the corresponding section. Sure, theming is gone, but providing content is much more important.

While I chose to go with a single-page approach, it’s also possible to serve the sections as separate pages and set [data-theme] on the server side. 

Another approach

So far, we’ve assumed that color values change linearly and are thus subject to a mathematical approach. But even in situations where this is only partially true, we may still be able to benefit from the same concept. For instance, if lightness follows a pattern but hue doesn’t, we could split up the stylesheet like this:

    :root {
      --hue: 260;
  <link rel="stylesheet" href="">

Supporting web components

Web components are an exciting (and evolving) concept. It’s enticing to think we can have encapsulated components that can be reused anywhere and theme them on a case-by-case basis. One component with many contexts!

We can use CSS variable theming with web components. It requires us to use a host-context() pseudo-selector. (Thanks to habemuscode for pointing this out to me!)

:host-context(body[data-theme="color-1"]) {
  --shade-1: var(--outsideHSL);

In summary…

Theming a website with CSS custom properties is much easier than the workaround approaches we’ve resorted to in the past. It’s more maintainable (one stylesheet), performant (less code), and opens up new possibilities (using JavaScript). Not to mention, CSS custom properties become even more powerful when they’re used with HSL colors and the calc() function.

We just looked at one example where we can change the color theme of a component based on the section where it is used. But again, there is much more opportunity here when we start to get into things like letting users change themes themselves – a topic that Chris explores in this article.

Source link

Executing project

Alexa Skill With TypeScript – DZone Web Dev

Alexa Skills can be developed using Alexa Lambda functions or a REST API endpoint. A Lambda Function is Amazon’s implementation of serverless functions available in AWS. Amazon recommends using Lambda Functions despite the fact that they are not easy to debug. While you can log to a CloudWatch log, you can’t hit a breakpoint and step into the code.

This makes the live-debugging of Alexa requests a very hard task. In this post, we will implement a custom Skill for Amazon Alexa by using TypeScript, npm, and AWS Lambda Functions. This skill is basically a Hello World example. With this post, you will be able to create a custom Skill for Amazon Alexa, implement functionality by using TypeScript, and start your custom Skill both from your local computer and from AWS. This post contains materials from different resources that can be seen in the Resources section.


Here you have the technologies used in this project

  1. Amazon Developer Account – How to get it.
  2. AWS Account – Sign up here for free.
  3. ASK CLI – Install and configure ASK CLI.
  4. Node.js v10.x.
  5. TypeScript (Version >3.0.0).
  6. Visual Studio Code.
  7. npm Package Manager.
  8. Alexa ASK for Node.js (Version >2.7.0).
  9. ngrok.

The Alexa Skills Kit Command Line Interface (ASK CLI) is a tool for you to manage your Alexa Skills and related resources, such as AWS Lambda functions. With ASK CLI, you have access to the Skill Management API, which allows you to manage Alexa skills programmatically from the command line. We will use this powerful tool to create, build, deploy, and manage our Hello World Skill, but now, with TypeScript. Let’s start!

Creating the Skill With ASK CLI

If you want how to create your Skill with the ASK CLI, please follow the first step explained in my Node.js Skill sample.

Once we have created the Skill in Node.js, we have to rewrite, or transpile, our code to TypeScript. I have made that work for you. Let’s take a look at it!

Project Files

These are the main files of the project:

  • .ask: Folder that contains the ASK CLI’s config file. This config files will remain empty until we execute the command ask deploy
  • .vscode/launch.json: Launch preferences to run locally your Skill for local testing. This setting launch lambda/custom/src/local-debugger.js. This script runs a server on http://localhost:3001 for debugging the Skill. It is not traspilled to TypeScript because it is not a part of our Lambda. It is a local tool.
  • hooks: A folder that contains the hook scripts. Amazon provides two hooks, post_new_hook and  pre_deploy_hook:
    • post_new_hook: executes after Skill creation. Node.js runs npm install in each sourceDir in skill.json
    • pre_deploy_hook: executed before the Skill deployment. Node.js runs npm install in each sourceDir in skill.json as well.
  • lambda/custom/src: A folder that contains the source code for the skill’s AWS Lambda function:
    • index.ts: the lambda main entry point.
    • package.json: this file is core to the Node.js ecosystem and is a basic part of understanding and working with Node.js, npm, and even modern TypeScript.
    • tsconfig.json: configuration file that we are going to use for compiling our TypeScript code.
    • tslint.json: configuration file used by gts (Google TypeScript Style) to check the style of our TypeScript code.
    • local-debugger.js: used for debugging our Skill locally.
    • errors: Folder that contains all error handlers.
    • intents: This one contains all the intent handlers.
    • interceptors: Interceptors’ folder with the i18n initialization.
    • utilities: This folder contains the i18n strings, helper functions, constants and TypeScript interfaces.
    • build: The output folder after compiling the TypeScript code.
  • models – A folder that contains interaction models for the skill. Each interaction model is defined in a JSON file named according to the locale. For example, es-ES.json.
  • skill.json – The Skill manifest. One of the most important files in our project.

Lambda Function in TypeScript

The ASK SDK for Node.js makes it easier for you to build highly engaging Skills by allowing you to spend more time implementing features and less time writing boilerplate code.

We are going to use this SDK in TypeScript!

You can find documentation, samples, and helpful links in their official GitHub repository.

The main TypeScript file in our Lambda project is index.ts, located in the lambda/custom/src folder. This file contains all handlers, interceptors, and exports the Skill handler in exports.handler.

The exports.handler function is executed every time AWS Lambda is initiated for this particular function. In theory, an AWS Lambda function is just a single function. This means that we need to define dispatching logic so a single function request can route to appropriate code, hence the handlers.

It is important to take a look into the Launch.ts, imported as Launch above, which is the LaunchRequestHandler handler located in the intents folder as an example of Alexa Skill handler written in TypeScript:

Building the Skill With Visual Studio Code

Inside package.json, we will almost always find metadata specific to the project. This metadata helps identify the project and acts as a baseline for users and contributors to get information about the project.

Here is how this file looks:

With TypeScript, we have to compile our code to generate JavaScript. For building our Skill, we can run the following command:

This command will execute these actions:

  1. Remove the build folder located in lambda/custom with the command rimraf build. This folder contains the output of compiling the TypeScript code.
  2. Check the style of our TypeScript code with the command gts check using the file tslint.json
  3. Compiles the TypeScript and generates the JavaScript code in the output folder lambda/custom/build using the command tsc --build tsconfig.json --pretty
  4. Copy the package.json to the build folder because is needed to generate the final Lambda code
  5. Finally, it will run the npm install --production in build folder to get the final lambda code that we are going to upload to AWS with the ASK CLI.

As you can see, this process in a TypeScript environment is more complex than in JavaScript.

Running the Skill With Visual Studio Code

The launch.json file in .vscode folder has the configuration for Visual Studio Code, which allows us to run our Lambda locally:

This configuration file will execute the following command:

This configuration uses the local-debugger.js file, which runs a TCP server listening on http://localhost:3001.

For a new incoming Skill request, a new socket connection is established. From the data received on the socket, the request body is extracted, parsed into JSON, and passed to the Skill invoker’s Lambda handler. The response from the Lambda handler is parsed as an HTTP 200 message format, as specified here. The response is written onto the socket connection and returned.

After configuring our launch.json file and understanding how the local debugger works, it is time to click on the play button:

Executing project

Executing project

After executing it, you can send an Alexa POST requests to http://localhost:3001.

Debugging the Skill With Visual Studio Code

Following the steps before, now you can set up breakpoints wherever you want inside all TypeScript files in order to debug your Skill.

Debugging the Skill

Debugging the Skill

Testing Requests Locally

I’m sure you already know the famous tool, Postman. REST APIs have become the new standard in providing a public and secure interface for your service. Though REST has become ubiquitous, it’s not always easy to test. Postman makes it easier to test and manage HTTP REST APIs. Postman gives us multiple features to import, test, and share APIs, which will help you and your team be more productive in the long run.

After running your application, you will have an endpoint available at http://localhost:3001. With Postman, you can emulate any Alexa Request.

For example, you can test a LaunchRequest:

Deploying Your Alexa Skill

With the code ready to go, we need to deploy it on AWS Lambda, so it can be connected to Alexa. Before deploying the Alexa Skill, we can show the config file in the .ask folder. It is empty:

Deploy Alexa Skill with ASK CLI:

As the official documentation says:

When the local skill project has never been deployed, ASK CLI creates a new Skill in the development stage for your account, then deploys the Skill project. If applicable, ASK CLI creates one or more new AWS Lambda functions in your AWS account and uploads the Lambda function code. Specifically, ASK CLI does the following:

  1. Looks in your skill project’s config file (in the .ask folder, which is in the skill project folder) for an existing skill ID. If the config file does not contain a Skill Id, ASK CLI creates a new Skill using the Skill manifest in the Skill project’s skill.json file. Then, it adds the Skill Id to the Skill project’s config file.
  2. Looks in your Skill project’s manifest (skill.json file) for the Skill’s published locales. These are listed in the manifest.publishingInformation.locales object. For each locale, ASK CLI looks in the Skill project’s models folder for a corresponding model file (for example, es-ES.json). It then uploads the model to your Skill. ASK CLI waits for the uploaded models to build and then adds each model’s eTag to the skill project’s config file.
  3. Looks in your Skill project’s manifest (skill.json file) for AWS Lambda endpoints. These are listed in the manifest.apis..endpoint or manifest.apis..regions..endpoint objects (for example, manifest.apis.custom.endpoint or manifest.apis.smartHome.regions.NA.endpoint).

    Each endpoint object contains a sourceDir value and optionally a URI value. ASK CLI uploads the contents of the sourceDir folder to the corresponding AWS Lambda function and names the Lambda Function the same as the URI value. For more details about how ASK CLI performs uploads to Lambda, see AWS Lambda deployment details.

  4. Looks in your Skill project folder for in-skill products, and if it finds any, uploads them to your Skill. For more information about in-skill products, see the In-Skill Purchasing Overview.

After the execution of the above command, we will have the config file properly filled:

Note: after rewriting our code to TypeScript, we need to change the codeUri from lambda/custom to lambda/custom/build because of our code compiled from TypeScript to JavaScript goes to the output folder build.

Test Requests Directly From Alexa

Ngrok is a very cool, lightweight tool that creates a secure tunnel on your local machine, along with a public URL you can use for browsing your local site or APIs.

When ngrok is running, it listens on the same port that your local web server is running on and proxies external requests to your local machine

From there, it’s a simple step to get it to listen to your web server. Say you’re running your local web server on port 3001. In terminal, you’d type in: ngrok http 3001. This starts ngrok listening on port 3001 and creates the secure tunnel:

Creating secure tunnel

Creating a secure tunnel

So now you have to go to Alexa Developer console, go to skill > endpoints > https, add the HTTPS URL generated above (e.g.

Select the My development endpoint as a sub-domain option from the dropdown and click save endpoint at the top of the page.

Go to the Test tab in the Alexa Developer Console and launch your Skill.

The Alexa Developer Console will send an HTTPS request to the ngrok endpoint (, which will route it to your Skill running on a Web API server at http://localhost:3001.



This was a basic tutorial to learn Alexa Skills using Node.js and TypeScript. As you have seen in this example, the Alexa Skill Kit for Node.js and the Alexa Tools like ASK CLI can help us a lot. They also give us the ability to create Skills in TypeScript in an easy way. I hope this example project is useful to you.

That’s all, folks! You can find all the code in my GitHub.

I hope it will be useful! If you have any doubts or questions, do not hesitate to contact me or put a comment below!

Happy coding!

Source link

Thank You, Christopher Schmitt | CSS-Tricks

Thank You, Christopher Schmitt | CSS-Tricks

It’s incredibly sad that Christopher Schmitt passed away last week¹. I keep thinking about how Christopher was one of the best dudes I knew. Just incredibly kind and thoughtful all the way through. I know everyone says that about people after they pass, but I really mean it here. I’m sure we all know people that we like quite a bit, but hey, we know they can be an asshole sometimes too. Not Christopher, at least not to me. He was always good to me in ways I’ll never be able to reciprocate.

Here’s the most important thing:

We (a group of developer friends) would like to build a site of memories that people have of Christopher. We have our own, but we’d like to collect as many memories from as many people as possible to make this a true memorial to him. If you have one you would like to share publicly, please submit it to this little site just for that.

I hardly know where to start reminiscing about Christopher. Broadly: I spoke at more conferences thrown by Christopher (and Ari) than anyone else. At least a dozen. He used to throw on called In Control in Orlando that I spoke at a number of times and met some early web friends that I’m still close with today! Stephanie Rewis mentioned to me that’s where we met the first time and she remembers me coding away on a yet-to-be released CodePen while there. In Control went to Hawaii one year, along with the first-ever CSS Dev Conf, brining me to Hawaii for the first time in my life, where I remember driving around the island with Daniel Burka. CSS Dev Conf went all sorts of amazing places, and I went to all of them! Estes Park, New Orleans, The Queen Mary… all these incredible locations that were all incredible memories for me.

And all I have are blurry, distant photos of him like this:

Christopher and Ari also ran an absolute ton of online conferences. CSS Summit, RWD Summit, SVG Summit… they ran a ton of them and they were really ahead of their time. I remember a funny moment where I had just moved to a new city and my internet wasn’t installed yet, but I still had to present online at one of these, so I did it out of my neighbors garage (as their kids slept), and the light in the garage kept going out because it didn’t detect movement. 😬

The people I met through Christopher are too many to count. He was a great connector in that way. His influence on the web was tremendous through his direct work, like writing (what, a dozen books?), blogging (even here!), speaking, and podcasting (e.g. The Non Breaking Space Show) but when you consider the people he brought together, his influence is immeasurable.

I literally made money off the guy. In the early years of ShopTalk, when it was tough to sell podcast spots at all, Christopher would sponsor every episode of ShopTalk, which probably gave us the boost that ensured it’s still happening today. Who knows what would have happened without that.

One of the things I liked best about Christopher is that he was a damn nerd. Through and through. In a way that made you envious that you could one day hope to up your nerd level that high. Guy had a Chewbacca costume that he wore regularly. His Instagram is full of snaps of him meeting other famous nerds.

I hope you still got those black Converses on wherever you are, buddy.

(And remember, if you have a memory with Christopher, please share it.)

Source link

Jetpack Instant Search! | CSS-Tricks

Jetpack Instant Search! | CSS-Tricks

Jetpack has had a search feature for a while. Flip it on, and it replaces your built-in WordPress search (which is functional, but not particularly good) with an Elasticsearch-powered solution that is faster and has better results. I’ve been using that for quite a while here on CSS-Tricks, which was an upgrade from using a Google Custom Search Engine.

Jetpack just upped their game again with a brand new search upgrade. You can use Jetpack search how you were already, or you can flip on Instant Search and take advantage of this all-new search experience (inside and out).

Flipping on Jetpack Instant Search from Jetpack Settings

A Full Page Experience

Instant Search provides a full page-covering search experience. I think it’s awesome. When a user is searching, that’s the mindset they are in and giving them all the space they need to accomplish that goal is great. Here’s me searching (video):

You get a full page experience on mobile as well.

Best I can tell, CSS-Tricks gets a couple hundred thousand on-site searches per month, so having a great experience there is very important to me. I don’t even wanna mess around with bad on-site search experiences, or products that are too expensive. I’d rather send people to a site-scoped Google search than a bad on-site search. Fortunately, Instant Search is about as good of an on-site search experience as I can imagine, especially for the zero-work it takes to implement.

Design Control

You have some control over the look of things from the Customizer.

Instant Search is designed to work on any site, so you probably don’t need to do much. I was really surprised how well it worked out-of-the-box for CSS-Tricks. As a CSS control freak, I did ship a handful of design tweaks to it, but that’s just because I love doing that kind of thing.

Tweaks No Longer Needed

With the previous version of Jetpack Search, I had custom code in place to tweak Elasticsearch. I did things like factoring in comment counts as an indicator of popularity, so that I could be sure our best content was high in the results. Remember as powerful as this search is, it doesn’t have a model of the entire internet to calculate relevancy from like Google does. Good news though:

To further improve our search algorithm, we started experimenting with adding the percentage of pageviews from the past 30 days into the index. We ended up finding that pageviews are a much better ranking signal because it somewhat combines both popularity and recency. So now most of our result ranking is strongly influenced by the number of pageviews a post or page gets. Conveniently, if you get a lot of Google Search traffic, our search results should be heavily influenced by Google’s ranking algorithm.

Emphasis mine. With Jetpack Instant Search, I was able to rip all that custom code out (removing code always feels great) because the new algorithms are doing a great job with ranking results.


Now Jetpack Search is ala-carte rather than baked into specific plans. Don’t need it, you don’t pay for it? Only need this feature? You can buy it regardless of what plan you are on.

I’m told the pricing is about scope. Jetpack plans are about features, not scale of site, but that doesn’t make much sense for search where the scale of the site matters a ton. So it’s a sliding scale based on the “records” you have, which are basically posts and pages.

Sliding scale of pricing

I would think a lot of sites fall into the $25/month (15% off for annual) category. You probably mostly start caring about on-site search above 1,000 records and 10,000 records is a ton. I pay for the tier one up from that (~$612 a year) only because our (now archived) bbPress forums pushes the number over 10,000. That’s a perfectly fair price for this for a site like mine.

Wish List

My #1 thing is that I wish it was easy to remove certain things from search results. We have tons and tons of records from our bbPress forums that I made the (hard) call to close this year. Removing those records would pull me down into a smaller pricing tier, but more importantly, I’d rather just not show those results in search at all.

It’s not just CSS-Tricks being in an unusual situation. I’ve also turned on Jetpack Instant Search on the CodePen Documentation.

In that circumstance, I’d consider turning removing blog posts (believe it or not) from the search results, so instead just the Pages would show up which are our core documentation there. Or perhaps even better, blog posts are just turned off as a filter by default, but users could flip them on to see them in the results.

All in all, this is a huge upgrade to Jetpack and yet another reason I consider it the most important plugin I run on my WordPress sites. If you’re curious about other Jetpack features we use, we made a special page for that.

Source link

Creating a new WebAssembly project

Learn WebAssembly With WebAssembly Studio


As web technologies have advanced, there’s been a push to move more and more applications to the web. This has presented developers with another challenge because web browsers support only one programming language: JavaScript. As browser makers looked for ways to improve JavaScript’s performance, they came up with a great new invention: WebAssembly.

WebAssembly was first introduced in 2015, and, alongside HTML, CSS, and JS, it became the fourth language for the Web that runs natively in browsers (W3C announced on  5 December 2019 ).

What Is WebAssembly?

As mentioned, one of the biggest issues that WebAssembly aims to solve is performance. JavaScript is a great language, but it is also known as an interpreted programming language and optimizations are always difficult with interpreted languages.

WebAssembly is a way of taking code written in programming languages other than JavaScript and running that code in the browser (by Lin Clark ). Currently, C, C++, and Rust were given focus as languages that could target WebAssembly.

The WebAssembly Core Specification describes WebAssembly as a safe, portable, low-level code format designed for efficient execution and compact representation.

There are lots of websites and documents explained about WebAssembly, so in this article, I will only introduce WebAssembly through implementing an extremely simple project using WebAssembly Studio.

A Project Demo

There are many tools to convert C, C++, or Rust snippets into WebAssembly. In this demo, I will explain how to convert C snippets into WebAssembly and use them in a simple project by using the WebAssembly Studio online tool. You can also see examples for C++ and Rust.

First, you access . Its interface looks like this:

Creating a new WebAssembly project

Creating a new WebAssembly project

In the Create New Project view, select Empty C Project and click the Create button. We will be sent to the following window:

New C project

New C project

There are a lot of items in this window, however, as a beginner, we just want to focus on the following functions. (Areas of focus are highlighted with yellow borders and numbers):

Areas of focus

Areas of focus
  • Area 1: Build and run our project.
  • Area 2: Save the content that we created.
  • Area 3: Our project contains three main files: the  main.c  file contains C code, the  main.html  contains HTML markup, and the  main.js  contains JS code.
  • Area 4: Display the compiling process.
  • Area 5: Things will be displayed in the browsers.

Next, we click the  main.c  in the Area 3 and look at its default content:

Change the code so we get this:

We defined two functions add  and  sub  and we also note that, these functions must be marked by  WASM_EXPORT  attributes. This implies that our functions will be converted into WebAssembly. Click the Save button in the Area 2.

Next, also in the Area 3, we click the main.html. Delete the existing HTML markups and replace them with the following markups:

Click the Save button in Area 2.

Finally, we click the  main.js  in the Area 3. We replace the statement:

with the following statements:

And don’t forget to click the Save button in Area 2.

Well done! So far, we can build and run our project by clicking the Build & Run button in Area 1, and we can also note things that would be displayed in Area 4 and 5:

Building and running application

Building and running application

There is a new file below our Area 3:

Newly created file

Newly created file

Here is the WebAssembly file, and you can see its content in the right view. We note export "add" and  export "sub" , which are two of our C functions (add and sub ) are converted into WebAssembly. 

You can download and use this file in your local project by clicking the Download:

Downloading file

Downloading file


In this article, I introduced an online tool that will help us to learn and work with WebAssembly easily. WebAssembly is a great tool, and you can discover more about it by referring to the WebAssembly website. Happy coding!

Source link

Post image

Do any of you guys want to pass time by contributing a logo …

You guys are skilled and I totally understand if you don’t want donate services. I came up with the idea of starting a specific blog while on “lockdown”. Someone is sending me a really cool instrument for producing music and my blog will be a video blog (or mostly videos) documenting me getting it and learning how to use it. I am calling it Quarintined Production (because we are basically quarintined and I am producing music).

Below are images of what I will be using and the blog will be documenting me learning.

I know this isn’t much to go on. If anyone is interested and wants to ask me anything, shoot me a message.

You guys are all so talented and have unique styles.

Post image
Post image
Post image
Post image
Post image

Source link