亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Abstract layer triangle
Same technology, different environments
Private environment
Standard environment
Web Components and Compositions
Web Components and Accessibility
in conclusion
Home Web Front-end CSS Tutorial Making Web Components for Different Contexts

Making Web Components for Different Contexts

Apr 21, 2025 am 09:29 AM

Making Web Components for Different Contexts

This article is not about how to build web components, Caleb Williams has recently written a detailed guide. Let's discuss how to use them, what factors need to be considered when building, and how to effectively utilize them in your project.

If you are new to web components, Caleb's guide is well worth reading, but here are some resources that can help you get started quickly:

  • Web Components – The "correct" approach
  • Shadow DOM v1: Self-contained Web Components
  • Web Components on MDN
  • Awesome Web Components
  • Recommended open web components

With web components now widely supported (thanks to the hard work of many behind the scenes) and considering that Edge will soon switch to the Chromium platform – people now see web components as a “native” and platform-compatible way to build reusable UI components to maintain consistency between design systems and web projects while leveraging the power of Shadow DOM to encapsulate styles and logic in the component itself.

This is both right and wrong. But first, let's take a look at the abstract layer triangle .

Abstract layer triangle

Technically, we should consider Web components as an extension of our favorite markup language HTML (yes!). The Web Component API allows us to create custom HTML elements that do not exist in HTML (for example<foo-bar></foo-bar> ).

Some say that web components are basically new HTML elements, so we should consider them as part of the HTML specification, so we should follow its paradigm, core concepts, and usage. If we assume all these points, we will find that our components will exist at the lowest level of the Web platform stack, side by side with HTML, CSS, and JavaScript. Frameworks and libraries (such as React, Vue, Angular, SvelteJS) work on their abstraction layer and sit on top of other tools that are in some kind of "middle-earth" such as StencilJs, Hybrids, and Lit. Beneath these abstraction layers, we can find our basic web technologies…and native web components. We can use the ALT ( Abstraction La ayer T riangle) diagram to represent this concept:

Why is this important? It helps us visualize the layers that exist on top of native components and understand their usage environment so that they can be built against the intended environment. What is the environment? That's our goal.

Same technology, different environments

Shadow DOM is a key factor in the Web Component API. It allows us to bundle JavaScript and CSS into custom elements to prevent external interference and style operations unless we explicitly allow it. Developers can indeed follow some ways to allow external CSS to leak into shadow root and components, including custom properties as well as ::part and ::theme pseudo-elements, which Monica Dinculescu has a good introduction.

Another thing to consider: the environment we are using. Three years after I personally built my web components, I can identify two environments: private environments (such as design systems) and standard environments (such as plain HTML, CSS, and JavaScript without custom styles).

Before designing components, we need to understand how they will be used, so determining the environment type is key to all of this. The easiest way is to target only one environment, but with a small CSS trick, we can build components for both .

Before we dig deeper, let's look at the difference between the two environments.

Private environment

A private environment is a closed ecosystem that provides components with their own styles and must be used as is. So if we are building a library of components from a specific style guide or design system, each component will reflect a custom style, so there is no need to encode it every time you need it.

The same is true for JavaScript logic. For example, we can attach a closed shadow root to prevent others from accidentally piercing the shadow boundary with querySelector. Therefore, we can simply select and use any component to avoid problems such as inconsistency in styles and CSS conflicts. As an author, you can also define a set of CSS custom properties (or ::parts) that can be used to style components for a specific use case, but this is not the focus of designing the system.

This is an example of using Web components in a private environment. All its styles and logic are contained in its shadow-root and can be simply added to any page.

This and subsequent examples are for demonstration purposes only and are not intended for production environments, as they do not take critical situations such as accessibility and other optimizations into account.

Components in private environments are rarely used outside of that environment. For example, if we try to get an element from a design system (with its own forced style), we can't simply add it to the project and expect to customize it. Do you know how Bootstrap can set up and customize the theme according to your preferences? This is the exact opposite. These components are intended to run in their environment and only in their environment.

Standard environment

Standard environments are probably the most complex component types, not only because environments can range from mature frameworks such as Vue and React to plain native HTML, but because everyone should be able to use the component as if it were any other element.

For example, when a component is added publicly (such as adding to npm), those who use it will expect to be able to customize it, at least to some extent.

Do you know of any HTML elements with their own demo style? The answer should be "no", because elements must be styled explicitly using CSS. The same is true for web components made in a standard environment. A single web component should be customizable by adding classes and properties or other methods.

This is the same as we see in the closed environment example<todo-list></todo-list> element, but it is designed for standard environments. It works as is, with no demo styles in its shadow root. In fact, it contains only the necessary logic and basic CSS to ensure its functionality. Otherwise, it can be customized like any standard HTML element (like a div).

The two examples we view for each environment were made using the same components. The difference is that components in a standard environment can be customized and selected using external CSS.

Web Components and Compositions

OK, so using web components is actually the same as using plain HTML, although as we can see, it is very important to follow the paradigms and principles of a given content. What we need to pay attention to is the combination of web components.

As Google Web Fundamentals explains:

Combinations are one of the least known features of Shadow DOM, but they are arguably the most important one.

In our web development world, composition is how we build applications declaratively using HTML. Different building blocks (<div> ,<code><s></s> ,<em></em> ,<strong></strong> ) put together to form an application. Some of these tags can even be used with each other. Combination is why<select></select> ,<details></details> ,<summary></summary> and<video></video> The reason why such native elements are so flexible. Each tag accepts certain HTML as child elements and performs some special operations on them. For example,<select></select> Know how to<option></option> and<optgroup></optgroup> Rendered as a drop-down list and a multi-select widget.<details></details> The element will<summary></summary> Presented as expandable arrows. even<video></video> Also know how to deal with certain child elements:<track></track> Elements are not rendered, but they do affect the behavior of the video. How amazing! Composition is what we usually do when using HTML. Since a web component is merely an HTML element with a DOM reference (not a logical container), we should rely on combinations to build our components and any subcomponents. If you consider

    and<select></select> , you'll notice that you declaratively combine these elements to get the final output, and you have specific properties that can be used for the main component (e.g. [readonly] ) or the child component (e.g. [selected] ). The same is true for web components, if you are building a custom list, consider building the main component (<custom-list></custom-list> ) and subcomponents (<custom-li></custom-li> ). use<slot></slot> Elements, you can define where the child elements will be placed, and the placeholder content that will be displayed when the child elements are not passed.

    Web Components and Accessibility

    Another thing to consider is what we call the “small” theme of accessibility . Since we are creating a completely new HTML element, we need to consider the accessibility of the element in order to provide basic semantic roles, any keyboard navigation, and user's operating system preferences such as reduced motion and high contrast settings.

    I strongly recommend the following resources as a reference for building accessible and inclusive components, how to define semantic tags, and how to implement basic keyboard navigation.

    • Inclusive Components
    • "Effect for everyone" on web.dev
    • WAI-ARIA Creative Practice
    • WebAIM WCAG Checklist

    in conclusion

    Web components are an emerging technology in web development, so there are no well-defined best practices to guide us when it comes to building them based on their intended use or maximizing use. When you find yourself using them, the only thing you might get from this post is to consider whether they are for closed or standard environments and ask yourself WHI :

    • W ho Will use this component?
    • H ow much flexibility This person should have to customize it?
    • Is this component suitable for everyone or for a specific audience?

The above is the detailed content of Making Web Components for Different Contexts. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
CSS tutorial for creating loading spinners and animations CSS tutorial for creating loading spinners and animations Jul 07, 2025 am 12:07 AM

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

Addressing CSS Browser Compatibility issues and prefixes Addressing CSS Browser Compatibility issues and prefixes Jul 07, 2025 am 01:44 AM

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

What is the difference between display: inline, display: block, and display: inline-block? What is the difference between display: inline, display: block, and display: inline-block? Jul 11, 2025 am 03:25 AM

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Creating custom shapes with css clip-path Creating custom shapes with css clip-path Jul 09, 2025 am 01:29 AM

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Styling visited links differently with CSS Styling visited links differently with CSS Jul 11, 2025 am 03:26 AM

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

How to create responsive images using CSS? How to create responsive images using CSS? Jul 15, 2025 am 01:10 AM

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

What are common CSS browser inconsistencies? What are common CSS browser inconsistencies? Jul 26, 2025 am 07:04 AM

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

Demystifying CSS Units: px, em, rem, vw, vh comparisons Demystifying CSS Units: px, em, rem, vw, vh comparisons Jul 08, 2025 am 02:16 AM

The choice of CSS units depends on design requirements and responsive requirements. 1.px is used for fixed size, suitable for precise control but lack of elasticity; 2.em is a relative unit, which is easily caused by the influence of the parent element, while rem is more stable based on the root element and is suitable for global scaling; 3.vw/vh is based on the viewport size, suitable for responsive design, but attention should be paid to the performance under extreme screens; 4. When choosing, it should be determined based on whether responsive adjustments, element hierarchy relationships and viewport dependence. Reasonable use can improve layout flexibility and maintenance.

See all articles