Giter VIP home page Giter VIP logo

Comments (5)

camsloanftc avatar camsloanftc commented on May 29, 2024

Hey @viperfx, not sure if you sorted this out, but here is how I handled this:

In my consumer app/website, I added a data attribute to a button. So in my case it was:

<button data-my-widget-id="123">Launch widget</button>

I added an ID because I need to have multiple triggers on the page to do different things, but if you only need one trigger you could also just set this attribute to be something like below:

<button data-my-widget-start="true">Launch widget</button>

Then inside my widget I do something like below:

const [isVisible, setIsVisible] = useState(false)

useEffect(() => {
    function startTour() {
      setIsVisible(true)
    }

    const startButton = document.querySelector('[data-my-widget-start]') 
    if(!startButton) return;
    
    startButton.addEventListener('click', startTour)
    
    return () => {
      startButton.removeEventListener('click', startTour)
    }
  }, [])

return (
<div>
  {isVisible && (<YourComponent />)}
</div>
)

I'm not sure if this is the "right" way. I was initially trying to pass this in via the habitat props, but it looks like the props are not set up to observe changes after mounting, so you would need to work around that.

Would love to hear how you or others solved this.

from preact-habitat.

viperfx avatar viperfx commented on May 29, 2024

@camsloanftc Thanks for the suggestion. I am curious how did you use useEffect? Are you using functional components with preact?

Would you be able to show how the function and export is setup? and did you have to change anything with the habitat render?

from preact-habitat.

camsloanftc avatar camsloanftc commented on May 29, 2024

@viperfx that's correct. So I basically have all that inside a functional component and export it.

// components/App.js
function App() {
  const [isVisible, setIsVisible] = useState(false)

  useEffect(() => {
    function startTour() {
      setIsVisible(true)
    }

    const startButton = document.querySelector('[data-my-widget-start]')
    if (!startButton) return

    startButton.addEventListener('click', startTour)

    return () => {
      startButton.removeEventListener('click', startTour)
    }
  }, [])

  return <div>{isVisible && <YourComponent />}</div>
}

export default App

Then I import that into my index file with the habitat initialization as normal. No changes required there:

import habitat from 'preact-habitat'
import App from './components/App'

const _habitat = habitat(App)

_habitat.render({
  selector: '[data-widget-host="habitat"]',
  clean: true,
})

from preact-habitat.

viperfx avatar viperfx commented on May 29, 2024

@camsloanftc Thanks for the reply! I am finding that mounting is not as reliable as I thought it would be across different websites.

I came across your podcast and app, and was wondering if you are using this library for your app in production?

Would love to chat sometime about widgets - in Ontario too!

from preact-habitat.

camsloanftc avatar camsloanftc commented on May 29, 2024

Hey @viperfx - thats cool that you stumbled across the podcast!

I am actually not in production yet so I will need to look out for the issues you’re describing. If that ends up being the case I will have to look at alternatives or see about diagnosing the problem via logs.

Definitely up for widget chats! Feel free to reach out by dm on twitter. Would be great to connect.

from preact-habitat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.