DEV Community

Cover image for DEV Profile Web Components: embed your dev.to profile anywhere
Scott Nath
Scott Nath

Posted on • Originally published at scottnath.com

DEV Profile Web Components: embed your dev.to profile anywhere

Learn about native web components that showcase DEV profiles and posts.

dev.to profile native web components, which show a user and their posts, are included in the profile-components library. These native web components can be utilized on any HTML page, framework-based site, or wherever you can use HTML. You can access them via unpkg.com or include the NPM module in your project.

tl;dr

install via NPM:

npm i profile-components
Enter fullscreen mode Exit fullscreen mode

use via unpkg.com:

<!-- add to HEAD -->
<script 
  type="module" 
  src="https://unpkg.com/profile-components/dist/devto-user.js">
</script>

<!-- shows a dev.to profile with posts fetched from dev.to for user `scottnath` -->
<devto-user username="scottnath" fetch="true"></devto-user>
Enter fullscreen mode Exit fullscreen mode

Quick links


Table of contents

What are the dev.to profile components?

  • Cross-browser, pure native web components
  • Zero dependencies
  • Fetch profile and post data from the dev.to API
  • No api key required
  • Present dev.to content as a profile widget
  • Styled using dev.to's CSS style variables from Forem
  • Released in the profile-components library

Includes two components: user and post

There are two components for presenting content from the dev.to site. One is a simple UI to present a single post, showing only an image and the post title and linking to the post. The other, devto-user, is the full profile component, which incorporates the devto-post UI to present user-written posts.

dev.to post

The devto-post web component displays details about a dev.to post. It is very basic, but can be styled via themes and adjusts spacing via container-queries.

dev.to post web component
dev.to post web component (400px)
dev.to post web component
dev.to post web component (300px)

dev.to user

The devto-user web component displays details about a DEV user. Using fetch to populate the content will include a set of posts (if the user has posts). On fetch, you can choose to not include posts by changing fetch="true" to fetch="no-posts".

dev.to user web component
dev.to user without posts
dev.to user
dev.to user (400px)

How to use the dev.to user profile component

These components can be used on any HTML page - whether built via framework or just plain HTML. They are available via unpkg.com or you can add the NPM module to your project.

Using the unpkg distribution for a User

Add the script tag to your HTML page's HEAD:

<!-- add to HEAD -->
<script
  type="module" 
  src="https://unpkg.com/profile-components/dist/devto-user.js">
</script>
Enter fullscreen mode Exit fullscreen mode

Add the component to your HTML page's BODY:

<!-- shows a user's profile -->
<devto-user username="scottnath" fetch="true"></devto-user>
Enter fullscreen mode Exit fullscreen mode

Show Fetched profile without posts

<!-- shows a user's profile without posts -->
<devto-user username="scottnath" fetch="no-posts"></devto-user>
Enter fullscreen mode Exit fullscreen mode

Write your own content instead of fetching from dev.to:

<devto-user
  username="scottnath"
  fetch="true"
  name="Meowy McMeowerstein"
  summary="Spending time purring and sleepin"
  profile_image="https://scottnath.com/profile-components/cat-square.jpeg"
  joined_at="Jan 1, 1979"
  post_count="1000000"
  latest_post='{...ForemPost}'
  popular_post='{...ForemPost}'
  data-theme="dark"
></devto-user>
Enter fullscreen mode Exit fullscreen mode

Server Side Rendering (Astro example)

Because these components were built with separate HTML, CSS, and JS files, you can use those pieces to generate HTML on the server. This example is what I did to make an Astro component for scottnath.com.

// DevToUser.astro
---
import devto from 'profile-components/devto-utils';
const user = devto.user;

const repos = JSON.stringify(['scottnath/profile-components', 'storydocker/storydocker']);
const userContent = await user.generateContent({
  login: 'scottnath',
  // a local profile image helps performance
  avatar_url: '/scott-nath-profile-pic.jpeg',
  repos
},true);
let userHTML = '<style>' + user.styles + '</style>';
userHTML += user.html(userContent);
---

<devto-user>
  <template shadowrootmode="open" set:html={userHTML}>
  </template>
</devto-user>
Enter fullscreen mode Exit fullscreen mode

Where do the styles come from?

The best way to have the look n feel of an external site is to integrate their design language as much as possible. The DEV web components use the same source for styles as dev.to itself, the Forem open source community software. More specifically, from the Forem open source repo on GitHub.

Forem Open source community software

https://github.com/forem/forem

This is a large codebase, which includes a lot of Ruby files. The styles are in both CSS and Sass files and can be found in the /app/assets/stylesheets subdirectory..

Dev, by way of Forem, essentially has four themes: Forem base styles, Dev branded styles, and light and dark versions of each. For now, these dev.to web components only compensate for dev.to branded colors. I wrote some scripts to copy-pasta specific variables from a few stylesheets which can be found in these docs for the dev.to web component helpers

Top comments (0)