28th Apr '23
/
2 comments

Move Color Palette Selector To Top in Color Popup

In Bricks Facebook group a user asks:

In the colors popup in the builder, I’d really like to move the color palette selector from the bottom of the popup to the top – makes a lot more sense to me to have it there…

Update

As Maxime points out here, JS is not needed at all as we can simply use the CSS order property.

While the below JS method is not necessary for the task at hand, it could still be a handy reference for when something that’s possible only with MutationObserver is to be implemented.

This tutorial shows how we can use MutationObserver interface to watch for changes to the DOM tree for running JavaScript when a div having a data attribute of control set to color is added in the DOM i.e., when the color picker is opened in the Bricks editor.

Before

After

Step 1

Install and activate the Bricks child theme if it is not already active.

Create a directory named assets inside the child theme directory and inside that another directory named js.

Create a file named say builder.js inside the above directory having this code:

document.addEventListener("DOMContentLoaded", () => {
  // Select the node that will be observed for mutations
  const targetNode = document.querySelector("#bricks-panel")

  // Options for the observer (which mutations to observe)
  const config = { childList: true, subtree: true }
  // childList: Set to true to monitor the target node (and, if subtree is true, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value is false.
  // subtree: Set to true to extend monitoring to the entire subtree of nodes rooted at target. All of the other properties are then extended to all of the nodes in the subtree instead of applying solely to the target node. The default value is false.

  // Callback function to execute when mutations are observed
  const callback = (mutationList, observer) => {
    for (const mutation of mutationList) {
      // console.log(mutation.target)
      if (
        mutation.target.hasAttribute("data-control") &&
        mutation.target.getAttribute("data-control") === "color"
      ) {
        const colorPaletteSelector = document.querySelector(
          ".color-palette-selector"
        )
        if (colorPaletteSelector) {
          const colorPaletteSelectorParent = colorPaletteSelector.parentElement
          colorPaletteSelectorParent.prepend(colorPaletteSelector)
        }
      }
    }
  }

  // Create an observer instance linked to the callback function
  const observer = new MutationObserver(callback)

  // Start observing the target node for configured mutations
  observer.observe(targetNode, config)

  // Create style tag and append to head
  document.head.appendChild(document.createElement("style")).innerHTML = `
    [data-control=color] .bricks-control-popup .color-modes {
        margin-bottom: 0;
    }
    
    [data-control=color] .bricks-control-popup .color-palette-selector {
        margin-bottom: 30px;
    }
    `
})

Step 2

Edit child theme’s functions.php.

Change

add_action( 'wp_enqueue_scripts', function() {
	// Enqueue your files on the canvas & frontend, not the builder panel. Otherwise custom CSS might affect builder)
	if ( ! bricks_is_builder_main() ) {
		wp_enqueue_style( 'bricks-child', get_stylesheet_uri(), ['bricks-frontend'], filemtime( get_stylesheet_directory() . '/style.css' ) );
	}
} );

to

add_action( 'wp_enqueue_scripts', function() {
	// Enqueue your files on the canvas & frontend, not the builder panel. Otherwise custom CSS might affect builder)
	if ( ! bricks_is_builder_main() ) {
		wp_enqueue_style( 'bricks-child', get_stylesheet_uri(), ['bricks-frontend'], filemtime( get_stylesheet_directory() . '/style.css' ) );
	} else {
		wp_enqueue_script( 'builder', get_stylesheet_directory_uri() . '/assets/js/builder.js', [], '1.0.0', true );
	}
} );

Thanks to cmstew and portseif for the JS code on which this is based.

Get access to all 630 Bricks code tutorials with BricksLabs Pro

2 comments

Leave your comment

 

Related Tutorials..

Custom keyboard shortcuts in Bricks builder

Custom keyboard shortcuts in Bricks builder

Learn how to insert common elements in Bricks builder editor easily with single key presses. Also, a handy hotkey to toggle the hover state for…
How to Create a Grid Slider in Bricks

How to Create a Grid Slider in Bricks

In this tutorial, we'll learn how to create multiple rows and columns inside the default nested slider of Bricks. Introduction A user recently asked in…
Categories:
Pro
[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

Setting up a sticky section that fades in when scrolling down and fades away when scrolled to the top.
Categories:
Pro
Opening Submenus with Click Instead of Hover in Bricks

Opening Submenus with Click Instead of Hover in Bricks

This Pro tutorial provides the code for opening submenus with click instead of hover in Bricks. Step 1 Add this in child theme's style.css: Step…
Categories:
How to change Ctrl/Cmd+Shift+V shortcut to View on frontend in Bricks

How to change Ctrl/Cmd+Shift+V shortcut to View on frontend in Bricks

Learn how to change the keyboard shortcuts for viewing on front end from Ctrl/Command + Shift + V to Ctrl/Command + Alt + V using…
Categories:
How to Change Bricks Preloader Background Color

How to Change Bricks Preloader Background Color

Working on a dark-themed site like our friend Storm and getting blinded by the light background of Bricks' preloader screen of the editor? Here's how…
Categories:
How to sync two nestable sliders in Bricks

How to sync two nestable sliders in Bricks

In this tutorial, we'll learn how to sync two nestable sliders in Bricks: one will be the main slider and the other one will be…
Categories:
How to change the text of an element based on breakpoints without creating duplicated content

How to change the text of an element based on breakpoints without creating duplicated content

In this tutorial, we'll learn how to dynamically change the text of a basic text element based on the viewport width in Bricks. Introduction Sometimes…
Categories: