Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Example of How to Use Custom Elements in Fresh #2741

Open
dhruv-dabhi-st7 opened this issue Oct 29, 2024 · 5 comments
Open

Add Example of How to Use Custom Elements in Fresh #2741

dhruv-dabhi-st7 opened this issue Oct 29, 2024 · 5 comments

Comments

@dhruv-dabhi-st7
Copy link

Description
Currently, the Deno Fresh documentation lacks an example of how to integrate and use a custom-created component that is not built into the Deno stack. This can be challenging for developers who want to extend their Fresh applications with custom components.

Proposed Solution:
Add a section in the documentation that provides a step-by-step guide on how to create and use a custom component in a Fresh project. The example should include:

  1. Creating a custom component file.
  2. Importing and using the custom component in a Fresh route.
  3. Best practices for organizing custom components within a Fresh project.
@marvinhagemeister
Copy link
Collaborator

What is a "custom component"? Do you mean a Preact component? If yes, then it looks like this:

function MyComponent() {
  return <h1>hello world</h1>
}

// usage
<MyComponent />

But not sure if this is what you're asking.

@dhruv-dabhi-st7
Copy link
Author

Steps to Use a Custom Component in a Fresh Project

  1. Create the Component
    • Write your component in HTML, CSS, and JavaScript.
    • Example: my-component.html.
<!-- my-component.html -->
<template id="my-component-template">
  <style>
    /* Add your CSS here */
    .my-component {
      color: red;
    }
  </style>
  <div class="my-component">
    <!-- Add your HTML here -->
    <p>Hello, I am a custom component!</p>
  </div>
</template>

<script>
  class MyComponent extends HTMLElement {
    constructor() {
      super();
      const template = document.getElementById('my-component-template').content;
      this.attachShadow({ mode: 'open' }).appendChild(template.cloneNode(true));
    }
  }

  customElements.define('my-component', MyComponent);
</script>
  1. Deploy the Component

    • Deploy my-component.html to a static hosting service (e.g., GitHub Pages, deno deploy).
    • Example URL: https://yourusername.github.io/my-component.html.
  2. Declare the Component in TypeScript

    • Add the component to the IntrinsicElements interface in your global.d.tsx file.
// types/global.d.tsx
declare module "preact" {
  namespace JSX {
    interface IntrinsicElements {
      "my-component": {
        flag?: string;
        label?: string;
        children?: preact.ComponentChildren;
        slot?: string;
      };
    }
  }
}
  1. Usage in Fresh
    • Include the script for your custom component in your HTML.
    • Use the component in your Fresh project with proper TypeScript declarations.
// routes/index.tsx
export default function Home() {
  return (
    <>
      <my-component flag="example"></my-component>
    </>
  );
}

This setup allows you to create, deploy, and use a custom component in a Fresh project.

@marvinhagemeister
Copy link
Collaborator

Oh I see, you are interested in using custom elements in fresh.

@marvinhagemeister marvinhagemeister changed the title Add Example of How to Use Custom Created Component in Fresh Add Example of How to Use Custom Elements in Fresh Oct 30, 2024
@dhruv-dabhi-st7
Copy link
Author

Oh I see, you are interested in using custom elements in fresh.

Yes so for that i suggest to add a section in the documentation that provides a step-by-step guide on how to create and use a custom elements as a component in a Fresh project

@falvarador
Copy link

Hello, I have the same interest. Today I use Astro with Custom Elements, but I have appreciation for Deno Fresh, so I would like to use it, but using Custom Elements.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants