-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
644ce99
commit b96045e
Showing
11 changed files
with
119 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import { animate } from "motion" | ||
|
||
// Connects to data-controller="rbui--accordion" | ||
export default class extends Controller { | ||
static targets = ['icon', 'content'] | ||
static values = { | ||
open: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
animationDuration: { | ||
type: Number, | ||
default: 0.15, // Default animation duration (in seconds) | ||
}, | ||
animationEasing: { | ||
type: String, | ||
default: 'ease-in-out', // Default animation easing | ||
}, | ||
rotateIcon: { | ||
type: Number, | ||
default: 180, // Default icon rotation (in degrees) | ||
}, | ||
} | ||
|
||
connect() { | ||
// Set the initial state of the accordion | ||
let originalAnimationDuration = this.animationDurationValue | ||
this.animationDurationValue = 0 | ||
this.openValue ? this.open() : this.close() | ||
this.animationDurationValue = originalAnimationDuration | ||
} | ||
|
||
// Toggle the 'open' value | ||
toggle() { | ||
this.openValue = !this.openValue | ||
} | ||
|
||
// Handle changes in the 'open' value | ||
openValueChanged(isOpen, wasOpen) { | ||
if (isOpen) { | ||
this.open() | ||
} else { | ||
this.close() | ||
} | ||
} | ||
|
||
// Open the accordion content | ||
open() { | ||
if (this.hasContentTarget) { | ||
this.revealContent() | ||
this.hasIconTarget && this.rotateIcon() | ||
this.openValue = true | ||
} | ||
} | ||
|
||
// Close the accordion content | ||
close() { | ||
if (this.hasContentTarget) { | ||
this.hideContent() | ||
this.hasIconTarget && this.rotateIcon() | ||
this.openValue = false | ||
} | ||
} | ||
|
||
// Reveal the accordion content with animation | ||
revealContent() { | ||
const contentHeight = this.contentTarget.scrollHeight; | ||
animate(this.contentTarget, { height: `${contentHeight}px` }, { duration: this.animationDurationValue, easing: this.animationEasingValue }) | ||
} | ||
|
||
// Hide the accordion content with animation | ||
hideContent() { | ||
animate(this.contentTarget, { height: 0 }, { duration: this.animationDurationValue, easing: this.animationEasingValue }) | ||
} | ||
|
||
// Rotate the accordion icon 180deg using animate function | ||
rotateIcon() { | ||
animate(this.iconTarget, { rotate: `${this.openValue ? this.rotateIconValue : 0}deg` }) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ui/accordion/accordion_default_content.rb → ...ui/accordion/accordion_default_content.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...ui/accordion/accordion_default_trigger.rb → ...ui/accordion/accordion_default_trigger.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
lib/phlex_ui/accordion/accordion_trigger.rb → lib/rbui/accordion/accordion_trigger.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters