Sha256: ea99b02dc1225b415e4273d33c2b575b23649c99702d067eaa31078be35df02d

Contents?: true

Size: 1.66 KB

Versions: 15

Compression:

Stored size: 1.66 KB

Contents

# backtick_javascript: true

class Link < Preact::Component
  EVENT_PUSH_STATE = "pushState"
  EVENT_REPLACE_STATE = "replaceState"

  self.context_type = RouterContext

  def initialize(props, context)
    super(props, context)
    if RUBY_ENGINE == 'opal'
      if `typeof window !== "undefined"`
        @history = `window.history`
      else
        @history = `{ length: 1, state: null, scrollRestoration: "auto", pushState: function(e,t,n){}, replaceState: function(e,t,n){} }`
      end
    end
    @rbase = nil
  end

  def navigate
    rto = props[:to] || props[:href]
    m = props[:replace] ? EVENT_REPLACE_STATE : EVENT_PUSH_STATE
    `#@history[m](null, "", rto[0] === "~" ? rto.slice(1) : #@rbase + rto)`
    @context[:router][:callback]&.call
  end

  def handle_click(event)
    # ignores the navigation when clicked using right mouse button or
    # by holding a special modifier key: ctrl, command, win, alt, shift
    if event.ctrl? || event.meta? || event.alt? || event.shift? || event.button > 0
      event.prevent
      return
    end
    props[:on_click]&.call(event)
    event.prevent unless event.prevented?
    navigate
  end

  render do
    to = props[:to]
    href = props[:href] || to
    children = props[:children]
    @rbase = @context[:router][:base]
    extra_props = {
      # handle nested routers and absolute paths
      href: href[0] === "~" ? href.slice(1) : @rbase + href,
      on_click: call(:handle_click),
      to: nil
    }
    # wraps children in `a` if needed
    a = children.is_a?(VNode) ? children : Preact.create_element("a", props)
    RPE(Preact.clone_element(a, extra_props))
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
isomorfeus-preact-23.9.0.rc12 lib/link.rb
isomorfeus-preact-23.9.0.rc11 lib/link.rb
isomorfeus-preact-23.9.0.rc10 lib/link.rb
isomorfeus-preact-23.9.0.rc9 lib/link.rb
isomorfeus-preact-23.9.0.rc8 lib/link.rb
isomorfeus-preact-23.9.0.rc7 lib/link.rb
isomorfeus-preact-23.9.0.rc6 lib/link.rb
isomorfeus-preact-23.9.0.rc5 lib/link.rb
isomorfeus-preact-23.9.0.rc4 lib/link.rb
isomorfeus-preact-23.9.0.rc3 lib/link.rb
isomorfeus-preact-23.9.0.rc2 lib/link.rb
isomorfeus-preact-23.9.0.rc1 lib/link.rb
isomorfeus-preact-23.8.0.rc3 lib/link.rb
isomorfeus-preact-23.8.0.rc2 lib/link.rb
isomorfeus-preact-23.8.0.rc1 lib/link.rb