Fix accordion toggle functionality with improved code organization and reliability

This commit is contained in:
Marcus Quinn
2025-03-16 01:30:43 +00:00
parent f17a80c078
commit 64ac2eb6e4

View File

@ -741,45 +741,91 @@ function wp_allstars_settings_page() {
</div> </div>
<style> <style>
/* Accordion Toggle Styles */
.wp-allstars-toggle {
background: #fff;
border: 1px solid #ccd0d4;
border-radius: 4px;
margin-bottom: 15px;
}
.wp-allstars-toggle-header {
padding: 15px;
}
.wp-allstars-toggle-main { .wp-allstars-toggle-main {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: flex-start; align-items: flex-start;
gap: 8px; gap: 8px;
} }
.wp-allstars-toggle label {
display: flex;
align-items: center;
gap: 10px;
margin: 0;
cursor: pointer;
}
.wp-allstars-expand-settings { .wp-allstars-expand-settings {
background: none; background: none;
border: none; border: none;
padding: 4px; padding: 4px;
cursor: pointer; cursor: pointer;
color: #2271b1; color: #2271b1;
transition: transform 0.2s ease;
} }
.wp-allstars-expand-settings:hover { .wp-allstars-expand-settings:hover {
color: #135e96; color: #135e96;
} }
.wp-allstars-expand-settings:focus { .wp-allstars-expand-settings:focus {
outline: 1px solid #2271b1; outline: 1px solid #2271b1;
box-shadow: none; box-shadow: none;
} }
.wp-allstars-expand-settings[aria-expanded="true"] .dashicons {
transform: rotate(180deg);
}
.wp-allstars-toggle-settings {
border-top: 1px solid #ccd0d4;
padding: 15px;
}
/* Description text */
.wp-allstars-toggle .description {
margin: 8px 0 0;
color: #646970;
}
</style> </style>
<script> <script>
jQuery(document).ready(function($) { jQuery(document).ready(function($) {
$('.wp-allstars-expand-settings').on('click', function() { // Store references to frequently accessed elements
var $button = $(this); const $expandButton = $('.wp-allstars-expand-settings');
var $settings = $button.closest('.wp-allstars-toggle').find('.wp-allstars-toggle-settings'); const $toggleSettings = $('.wp-allstars-toggle-settings');
var isExpanded = $button.attr('aria-expanded') === 'true';
// Handle expand/collapse functionality
$expandButton.on('click', function(e) {
e.preventDefault();
e.stopPropagation();
// Toggle aria-expanded state const $button = $(this);
const isExpanded = $button.attr('aria-expanded') === 'true';
// Update button state
$button.attr('aria-expanded', !isExpanded); $button.attr('aria-expanded', !isExpanded);
// Toggle icon // Animate the settings panel
$button.find('.dashicons') $toggleSettings.stop().slideToggle(200);
.removeClass(isExpanded ? 'dashicons-arrow-up-alt2' : 'dashicons-arrow-down-alt2') });
.addClass(isExpanded ? 'dashicons-arrow-down-alt2' : 'dashicons-arrow-up-alt2');
// Prevent settings panel from closing when clicking inside it
// Toggle settings panel with animation $toggleSettings.on('click', function(e) {
$settings.slideToggle(200); e.stopPropagation();
}); });
}); });
</script> </script>