


Learn how to make a styled picture frame web component with props (Learn Modulo.js - Part f
? Welcome back! Didn't catch Part 1? No worries, you can start at the beginning, or just plunge in here!
Introducing: The PictureFrame Component
Our task in this tutorial will be to build a picture frame component, for styling photographs on a web app. Last time we ended with a snippet a little like the one below. However, in this tutorial, we've changed the "Template" to show instead a pink/salmon "picture frame" of a hippo with a caption below it. To begin this tutorial, copy and paste the following into a new file, and open in your browser:
<template modulo> <component name="PictureFrame"> <template> <div style="display: inline-block; border: 10px inset salmon; padding: 10px; margin: 10px; width: 100px; background: pink;"> <img src="/static/imghwm/default1.png" data-src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Hippo_walking.jpg/320px-Hippo_walking.jpg" class="lazy" style="max-width:90%" alt="Learn how to make a styled picture frame web component with props (Learn Modulo.js - Part f" > <p>Photograph: <em>The Return of the Hippo</em></p> </div> </template> </component> </template> <script src="https://unpkg.com/mdu.js"></script> <x-pictureframe></x-pictureframe>
Introducing Part 2
In this tutorial, we'll add a Style to plain HTML components, along with a discussion on the first core concept in the Modulo framework: Component Parts, before finally peeking at one more important component part: Props.
First, you might notice something messy about the above code. All the style is rammed into a style= attribute! When coding CSS, placing all our styles in inline style= attributes is often hard to maintain. Modulo supports the "Style" Component Part to let us write CSS code more naturally. What's a Component Part? Well, we'll get to that as well. First, let's get stylish!
Step 1: Creating a Style Component Part
<template> <div class="salmon-frame"> <img src="/static/imghwm/default1.png" data-src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Hippo_walking.jpg/320px-Hippo_walking.jpg" class="lazy" alt="Learn how to make a styled picture frame web component with props (Learn Modulo.js - Part f" > <p>Photograph: <em>The Return of the Hippo</em></p> </div> </template> <style> img { width: 50px; } .salmon-frame { display: inline-block; border: 10px inset salmon; padding: 10px; margin: 10px; width: 100px; background: pink; } </style>
Much better! This will look and behave much like a "Style" tag in HTML, allowing you to create classes and selectors to your heart's content.
Step 2: Styling the host component itself
Often, you'll want to style the Web Component itself (in this case, the
<style> :host { text-align: center; } /* ... etc ... */ </style>
Introducing Component Parts
The central concept to Modulo is that of Component Parts. All component definitions consist of some number of Component Parts. Thus, a component definition is really just a collection of Component Part definitions. "Under the hood" of your component, each Component Part will have a different role to contribute to the functionality of your component.
We've already learned the two most basic Component Part:
Template - - Templates are where you put any arbitrary HTML code that you want your component to contain. For now, we'll just include some unchanging HTML. In the next tutorial, we'll learn how to use "templating language" to control what HTML is produced in what circumstance.
Style -
Keep in mind both Style and Template have a lot of configurable options, but for our usage, the default is good enough.
Introducing Props
Now, let's introduce a third important Component Part: Props
In the previous section, we were mostly concerned with defining components. Recall that components are defined once, but can be used many times. The purpose of Props is to allow more flexibility in that second step: Props Component Part defines the properties that can be customized about a component each time it is reused.
Props to us!
Right now, our picture frame doesn't have a way to be re-used for different photos or captions. We can't just all be chased by hippos forever! ???
We want to eventually be able to do something like this:
<x-pictureframe image="https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Hippo_walking.jpg/320px-Hippo_walking.jpg" caption="Hippopotamus out of water" photographer="Lee R. Berger"></x-pictureframe>
Then, it'd feel like a "real" HTML tag -- that is, configurable via attributes. Let's "peel back the layers" and examine out how this can be accomplished using Props.
Step 3: Adding Props
In order for a component to be able to "receive" props when it is reused, we must define Props. Props definitions are like previous definitions, except they have no contents and are just an opening tag with attributes, followed by a closing tag. See the following:
<props image caption photographer></props>
Note: A common mistake is to forget to include Props. Every attribute you expect your component to receive must be listed in Props to be available for the next step.
Step 4: Using Props in Template
Now, we'll get our first sneak peak at the true power of Templates: Adding in data and values into your HTML. You can use the following syntax to get your "props" and inject them into the generated HTML:
<div class="salmon-frame"> <img src="/static/imghwm/default1.png" data-src="https://unpkg.com/mdu.js" class="lazy" alt="Learn how to make a styled picture frame web component with props (Learn Modulo.js - Part f" > <p>Photograph: <em>{{ props.caption }}</em></p> <p>Photographer: <em>{{ props.photographer }}</em></p> </div>
Note the {{ and }}: This the templating syntax that indicates where the "props" values get inserted.
- Complete Example
Combining it all, we get the following results. Note how our custom Web Component can be re-used with different attributes:
<props image caption photographer></props> <script></script>![]()
Photograph: {{ props.caption }}
Photographer: {{ props.photographer }}
Learn Modulo.js: Part 2
Style and Props
<x-pictureframe image="https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Hippo_walking.jpg/320px-Hippo_walking.jpg" caption="Hippopotamus out of water" photographer="Lee R. Berger"></x-pictureframe>
Conclusion
That's all for Part 2! Be sure to follow to catch the rest, and, as always, feel free to ask questions or suggestions in the comments.
The above is the detailed content of Learn how to make a styled picture frame web component with props (Learn Modulo.js - Part f. For more information, please follow other related articles on the PHP Chinese website!

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)
