-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(drawer): add rails expand/contract setup
- Loading branch information
1 parent
eec700f
commit 54cd3e0
Showing
7 changed files
with
129 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
class SageDrawer < SageComponent | ||
set_attribute_schema({ | ||
active: [:optional, NilClass, TrueClass], | ||
disable_background_blur: [:optional, NilClass, TrueClass], | ||
disable_background_dismiss: [:optional, NilClass, TrueClass], | ||
expanded: [:optional, NilClass, TrueClass], | ||
expanded_size: [:optional, NilClass, String], | ||
id: [:optional, NilClass, String], | ||
size: [:optional, NilClass, String], | ||
show_close: [:optional, TrueClass], | ||
title: [:optional, String], | ||
}) | ||
def sections | ||
%w(drawer_header) | ||
%w(drawer_header, drawer_expanded_area) | ||
end | ||
end |
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
25 changes: 21 additions & 4 deletions
25
docs/lib/sage_rails/app/views/sage_components/themes/legacy/_sage_drawer.html.erb
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,92 @@ | ||
import { forEach } from "lodash"; | ||
|
||
Sage.drawerExpandCollapse = (() => { | ||
// ================================================== | ||
// Variables | ||
// ================================================== | ||
|
||
const DRAWER_ANIMATION_TIMEOUT = 500; | ||
const CLASSNAME_DRAWER_EXPANDED = "sage-drawer--expanded"; | ||
const CLASSNAME_DRAWER_HIDDEN_CONTENT = "sage-drawer__hidden-content"; | ||
const SELECTOR_COLLAPSED_CONTENT = "[data-js-drawer-collapsed-content]"; | ||
const SELECTOR_EXPANDED_CONTENT = "[data-js-drawer-expanded-content]"; | ||
|
||
// ================================================== | ||
// Functions | ||
// ================================================== | ||
|
||
// Select the modal targeted by an expand or contract trigger | ||
const getTargetModal = (modalTarget) => document.querySelector(`[data-js-modal="${modalTarget}"`); | ||
|
||
// After a timer expires reveal elements within a given modal | ||
const showContentAfterTimer = ($elModal, selector) => { | ||
setTimeout( | ||
() => $elModal.querySelectorAll(selector).forEach( | ||
el => el.classList.remove(CLASSNAME_DRAWER_HIDDEN_CONTENT) | ||
), | ||
DRAWER_ANIMATION_TIMEOUT | ||
); | ||
}; | ||
|
||
// Immediately hide elements with a given a given modal | ||
const hideContentImmediately = ($elModal, selector) => $elModal | ||
.querySelectorAll(selector) | ||
.forEach( | ||
el => el.classList.add(CLASSNAME_DRAWER_HIDDEN_CONTENT) | ||
); | ||
|
||
// Process click on a expand trigger | ||
const expandDrawer = (evt) => { | ||
// Retrieve the modal target from the trigger | ||
const $elModal = getTargetModal(evt.target.dataset.jsDrawerExpand); | ||
|
||
// Add expanded class to the modal | ||
$elModal.classList.add(CLASSNAME_DRAWER_EXPANDED); | ||
|
||
// Immediately hide collapsed-only content | ||
hideContentImmediately($elModal, SELECTOR_COLLAPSED_CONTENT); | ||
|
||
// After animation finishes, reveal expanded-only content | ||
showContentAfterTimer($elModal, SELECTOR_EXPANDED_CONTENT); | ||
}; | ||
|
||
// Process click on a collapse trigger | ||
const collapseDrawer = (evt) => { | ||
// Retrieve the modal target from the trigger | ||
const $elModal = getTargetModal(evt.target.dataset.jsDrawerCollapse); | ||
|
||
// Remove expanded class from the modal | ||
$elModal.classList.remove(CLASSNAME_DRAWER_EXPANDED); | ||
|
||
// Immediately hide expanded-only content | ||
hideContentImmediately($elModal, SELECTOR_EXPANDED_CONTENT); | ||
|
||
// After animation finishes, reveal collapsed-only content | ||
showContentAfterTimer($elModal, SELECTOR_COLLAPSED_CONTENT); | ||
}; | ||
|
||
// Hides all expanded-only content right away | ||
const initExpandCollapse = (el) => { | ||
el.classList.add(CLASSNAME_DRAWER_HIDDEN_CONTENT); | ||
}; | ||
|
||
// Listens for clicks on expand triggers | ||
const initExpand = (el) => el.addEventListener("click", expandDrawer); | ||
|
||
// Removes listener for clicks on expand triggers | ||
const unbindExpand = (el) => el.removeEventListener("click", expandDrawer); | ||
|
||
// Listens for clicks on collapse triggers | ||
const initCollapse = (el) => el.addEventListener("click", collapseDrawer); | ||
|
||
// Removes listener for clicks on collapse triggers | ||
const unbindCollapse = (el) => el.removeEventListener("click", collapseDrawer); | ||
|
||
return { | ||
initExpandCollapse, | ||
initExpand, | ||
unbindExpand, | ||
initCollapse, | ||
unbindCollapse, | ||
}; | ||
})(); |
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