Skip to content

Commit

Permalink
Merge pull request #5451 from solidusio/elia/dynamic-toggletip
Browse files Browse the repository at this point in the history
Dynamic `ui/toggletip` positioning
  • Loading branch information
elia authored Oct 27, 2023
2 parents c0d7d95 + 2a631bc commit b653f69
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
14 changes: 0 additions & 14 deletions admin/app/components/solidus_admin/ui/panel/component.js

This file was deleted.

4 changes: 0 additions & 4 deletions admin/app/components/solidus_admin/ui/panel/component.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<%= tag.attributes(**@attributes) %>
>
<summary
type="button"
class="
block
w-[1rem]
Expand All @@ -12,9 +11,8 @@
[&::marker]:hidden
[&::-webkit-details-marker]:hidden
"
data-<%= stimulus_id %>-target="button"
data-action="
click:prevent-><%= stimulus_id %>#toggle
click-><%= stimulus_id %>#toggle
keydown.esc@window-><%= stimulus_id %>#close
"
aria-label="<%= t('.get_help') %>"
Expand Down Expand Up @@ -52,7 +50,6 @@
before:bg-inherit
<%= POSITIONS.fetch(@position)[:arrow] %>
"
data-<%= stimulus_id %>-target="content"
>
<%= @text %>
</span>
Expand Down
26 changes: 22 additions & 4 deletions admin/app/components/solidus_admin/ui/toggletip/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,43 @@ import { Controller } from '@hotwired/stimulus'
import { useClickOutside } from 'stimulus-use'

export default class extends Controller {
static targets = ['button', 'bubble', 'content']
static targets = ['bubble']

connect () {
useClickOutside(this)
this.open = false
}

clickOutside () {
this.close()
}

toggle (e) {
this.element.open = !this.element.open
e.preventDefault()
this.open = !this.open
this.render()
}

open () {
this.element.open = true
this.open = true
this.render()
}

close () {
this.element.open = false
this.open = false
this.render()
}

render() {
const needsPositioning = this.open && !this.element.open
this.element.open = this.open

if (needsPositioning) {
const bubbleRect = this.bubbleTarget.getBoundingClientRect()
if (bubbleRect.right > window.innerWidth) this.bubbleTarget.style.left = `${window.innerWidth - bubbleRect.width}px`
if (bubbleRect.bottom > window.innerHeight) this.bubbleTarget.style.top = `${window.innerHeight - bubbleRect.height}px`
if (bubbleRect.left < 0) this.bubbleTarget.style.left = '0px'
if (bubbleRect.top < 0) this.bubbleTarget.style.top = '0px'
}
}
}

0 comments on commit b653f69

Please sign in to comment.