Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from stimulus-components/feature/removing-mouseout
Browse files Browse the repository at this point in the history
Feature/removing mouseout
  • Loading branch information
guillaumebriday authored Nov 10, 2020
2 parents c4e9233 + 5a3d0d4 commit e436648
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.0.0] - 2020-11-10
### Added
- Adding `tippyOptions` getter to simply override default options.
- `tippy` instance is now a singleton.
- Destroy `tippy` instance on disconnect.

### Changed
**Breaking** - Removing `mouseOut` action.
**Breaking** - `popover` action does not return a new `tippy` instance.

## [1.0.0] - 2020-10-20

### Added
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ With server rendered content on the fly:
<a
href="/profile"
data-controller="popover"
data-action="mouseover->popover#mouseOver mouseout->popover#mouseOut"
data-action="mouseover->popover#mouseOver"
data-popover-url="<%= card_path %>"
>
profile card
Expand All @@ -69,7 +69,7 @@ With static html:
This is my Github card available on
<a
href="/profile"
data-action="mouseover->popover#mouseOver mouseout->popover#mouseOut"
data-action="mouseover->popover#mouseOver"
>
Github
</a>
Expand All @@ -86,7 +86,6 @@ With static html:
| --------- | ------- | ----------- | -------- |
| `data-popover-url` | `undefined` | URL to fetch the content. ||


## Extending Controller

You can use inheritance to extend the functionality of any Stimulus components.
Expand All @@ -98,6 +97,9 @@ export default class extends Popover {
connect() {
super.connect()
console.log("Do what you want here.")

// Get tippy options. Override this method if needed.
this.tippyOptions
}
}
```
Expand Down
5 changes: 1 addition & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ <h2 class="text-xl mb-2 mt-8">With AJAX</h2>
<h2 class="text-xl mb-2 mt-8">With static html</h2>
<div data-controller="popover">
This is my Github card available on
<a
href="https://github.com/stimulus-components/stimulus-popover"
data-action="mouseover->popover#mouseOver mouseout->popover#mouseOut"
>
<a href="https://github.com/stimulus-components/stimulus-popover" data-action="mouseover->popover#mouseOver">
Github </a
>.

Expand Down
33 changes: 18 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export default class extends Controller {
this.fetch = this.fetch.bind(this)
}

disconnect () {
if (this.tippyInstance) {
this.tippyInstance.destroy()
}
}

async mouseOver (event) {
let element = null
let content = null
Expand All @@ -22,12 +28,7 @@ export default class extends Controller {
content = this.remoteContent
}

this.tippyInstance = this.popover(element, content)
}

mouseOut () {
this.tippyInstance.destroy()
this.tippyInstance = undefined
this.popover(element, content)
}

async fetch () {
Expand All @@ -36,19 +37,21 @@ export default class extends Controller {
}

const response = await fetch(this.data.get('url'))
const html = await response.text()

this.remoteContent = html
this.remoteContent = await response.text()
}

popover (element, content) {
const instance = tippy(element, {
content: content,
allowHTML: true
})
if (!this.tippyInstance) {
this.tippyInstance = tippy(element, this.tippyOptions)
this.tippyInstance.show()
}

instance.show()
this.tippyInstance.setContent(content)
}

return instance
get tippyOptions () {
return {
allowHTML: true
}
}
}

0 comments on commit e436648

Please sign in to comment.