PikoPong
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux
No Result
View All Result
PikoPong
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux
No Result
View All Result
PikoPong
No Result
View All Result
Home Web Dev

How You Might Build a Modern Day Webring

November 20, 2020
in Web Dev
289 3
How You Might Build a Modern Day Webring


I’m sure different people picture different things when they think about webrings, so let me clarify what I picture. I see an element on a website that:

  1. Signifies this site is part of a webring
  2. Allows you to move to the next or previous site of the webring
  3. Maybe has other functionality like going to a “random” site or seeing the complete list

But then another major thing:

  1. Site owners don’t have to do much. They just plop (it?) on the site and a functional webring UI is there.

So like this:

A Calvin & Hobbes webring UI that comes up all the time when you search the web about webrings

How did it used to work? You know what? I have no idea. My guess is that it was an ancient <frameset><frame /></frameset> situation, but this stuff is before my time a bit. How might we do this today?

Well, we could use an <iframe>, I guess. That’s what sites like YouTube do when they give “embed code” as an HTML snippet. Sites like Twitter and CodePen give you a <div> (or whatever semantic HTML) and a <script>, so that there can be fallback content and the script enhances it into an <iframe>. An <iframe> might be fine, as it asks very little of the site owner, but they are known to be fairly bad for performance. It’s a whole document inside another document, after all. Plus, they don’t offer much by the way of customization. You get what you get.

Another problem with an iframe is that… how would it know what site it is currently embedded on? Maybe a URL parameter? Maybe a postMessage from the parent page? Not super clean if you ask me.

I think a Web Component might be the way to go here, as long as we’re thinking modern. We could make a custom element like <webring-*>. Let’s do that, and make it for CSS sites specifically. That’ll give us the opportunity to send in the current site with an attribute, like this:

<webring-css site="http://css-tricks.com">
  This is gonna boot itself up into webring in a minute.
</webring-css>

That solves the technology choice. Now we need to figure out the global place to store the data. Because, well, a webring needs to be able to be updated. Sites need to be able to be added and removed from the webring without the other sites on the webring needing to do anything.

For fairly simple data like this, a JSON file on GitHub seems to be a perfectly modern choice. Let’s do that.

Now everyone can see all the sites in the webring in a fairly readable fashion. Plus, they could submit Pull Requests against it to add/remove sites (feel free).

Getting that data from our web component is a fetch away:

fetch(`https://raw.githubusercontent.com/CSS-Tricks/css-webring/main/webring.json`)
  .then((response) => response.json())
  .then((sites) => {
     // Got the data.
  });

We’ll fire that off when our web component mounts. Let’s scaffold that…

const DATA_FOR_WEBRING = `https://raw.githubusercontent.com/CSS-Tricks/css-webring/main/webring.json`;

const template = document.createElement("template");
template.innerHTML = `
<style>
  /* styles */
</style>

<div class="webring">
  <!-- content -->
</div>`;

class WebRing extends HTMLElement {
  connectedCallback() {
    this.attachShadow({ mode: "open" });
    this.shadowRoot.appendChild(template.content.cloneNode(true));

    fetch(DATA_FOR_WEBRING)
      .then((response) => response.json())
      .then((sites) => {
        // update template with data
      });
  }
}

window.customElements.define("webring-css", WebRing);

The rest of this isn’t so terribly interesting that I feel like we need to go through it step by step. I’ll just blog-sketch it for you:

  1. Pull the attribute off the web component so we can see what the current site is
  2. Match the current site in the data
  3. Build out Next, Previous, and Random links from the data in a template
  4. Update the HTML in the template

And voilà!

Could you do a lot more with this, like error handling, better design, and better everything?

Yes.



Source link

Share219Tweet137Share55Pin49

Related Posts

You want minmax(10px, 1fr) not 1fr
Web Dev

You want minmax(10px, 1fr) not 1fr

There are a lot of grids on the web like this: .grid { display: grid; grid-template-columns: repeat(3, 1fr); }...

January 22, 2021
Servers: Cool Once Again | CSS-Tricks
Web Dev

Servers: Cool Once Again | CSS-Tricks

There were jokes coming back from the holiday break that JavaScript decided to go all server-side. I think it...

January 22, 2021
When To Say No To Freelance Projects — Smashing Magazine
Web Dev

When To Say No To Freelance Projects — Smashing Magazine

About The AuthorBecca is a UX Researcher with a PhD in Human Factors Psychology. She runs the UX consulting...

January 22, 2021
useStateInCustomProperties | CSS-Tricks
Web Dev

useStateInCustomProperties | CSS-Tricks

In my recent “Custom Properties as State” post, one of the things I mentioned was that theoretically, UI libraries, like...

January 21, 2021
Next Post
How to Save the Command Output to a File in Linux Terminal

How to Save the Command Output to a File in Linux Terminal

What’s Missing From CSS?

What's Missing From CSS?

Leave a Reply Cancel reply

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

Recommended

Linux Kernel 5.8 “Biggest Release of All Time” Released!

Linux Kernel 5.8 “Biggest Release of All Time” Released!

August 3, 2020
Theming and Theme Switching with React and styled-components

Theming and Theme Switching with React and styled-components

January 7, 2021

Best practices for measuring Web Vitals in the field

May 28, 2020
4 Lessons Web App Designers Can Learn From Google — Smashing Magazine

4 Ways To Creatively Use Icons In Your Mobile Apps — Smashing Magazine

September 3, 2020

Categories

  • AWS
  • Big Data
  • Database
  • DevOps
  • IoT
  • Linux
  • Web Dev
No Result
View All Result
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In