Class Drawer

All Implemented Interfaces:
ClientEventListener, MouseClickListener, EventListener

public class Drawer extends StyledElement<Drawer> implements MouseClickListener
A slide-out panel for mobile navigation or side content.

Drawer provides a panel that slides in from the edge of the screen, typically used for mobile navigation menus. It includes a backdrop overlay and can be positioned on the left or right side.

Features:

  • Left or right positioning
  • Configurable width
  • Semi-transparent backdrop
  • Smooth slide animation
  • Open/close via Java methods

Usage:


 // Create left drawer
 Drawer drawer = new Drawer();
 drawer.setWidth(280);
 drawer.addElement(navigationMenu);

 // Right drawer
 Drawer drawer = Drawer.right();
 drawer.addElement(settingsPanel);

 // Open/close from event handler
 hamburgerBtn.registerListener(this, MouseClickedEvent.class);

 public void onEvent(MouseClickedEvent event) {
     drawer.open();
 }

 // Close on backdrop click (handled automatically)
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Field Details

  • Constructor Details

    • Drawer

      public Drawer()
      Constructs a left-side drawer with default width.
    • Drawer

      public Drawer(Drawer.Side side)
      Constructs a drawer on the specified side.
      Parameters:
      side - the side from which the drawer slides
  • Method Details

    • initialize

      protected void initialize()
      Builds the element tree for the drawer.
      Overrides:
      initialize in class StyledElement<Drawer>
    • left

      public static Drawer left()
      Creates a left-side drawer.
      Returns:
      a new left drawer
    • right

      public static Drawer right()
      Creates a right-side drawer.
      Returns:
      a new right drawer
    • setWidth

      public Drawer setWidth(int widthPx)
      Sets the width of the drawer panel.
      Overrides:
      setWidth in class StyledElement<Drawer>
      Parameters:
      widthPx - the width in pixels
      Returns:
      this Drawer for method chaining
    • setWidth

      public Drawer setWidth(String width)
      Sets the width of the drawer panel.
      Overrides:
      setWidth in class StyledElement<Drawer>
      Parameters:
      width - the width value (e.g., "280px", "80%", "20rem")
      Returns:
      this Drawer for method chaining
    • setBackdropColor

      public Drawer setBackdropColor(String color)
      Sets the backdrop color.
      Parameters:
      color - the backdrop color (e.g., "rgba(0,0,0,0.5)")
      Returns:
      this Drawer for method chaining
    • setPanelBackground

      public Drawer setPanelBackground(String color)
      Sets the panel background color.
      Parameters:
      color - the panel background color
      Returns:
      this Drawer for method chaining
    • setZIndex

      public Drawer setZIndex(int zIndex)
      Sets the z-index for stacking control.
      Overrides:
      setZIndex in class StyledElement<Drawer>
      Parameters:
      zIndex - the z-index value
      Returns:
      this Drawer for method chaining
    • setPanelShadow

      public Drawer setPanelShadow(String shadow)
      Adds a box shadow to the panel.
      Parameters:
      shadow - the CSS box-shadow value
      Returns:
      this Drawer for method chaining
    • withShadow

      public Drawer withShadow()
      Adds a default shadow to the panel.
      Returns:
      this Drawer for method chaining
    • open

      public void open()
      Opens the drawer.

      This method updates the drawer state and sends the visual update to the client.

    • close

      public void close()
      Closes the drawer.

      This method updates the drawer state and sends the visual update to the client.

    • toggle

      public void toggle()
      Toggles the drawer open/closed state.
    • onEvent

      public void onEvent(MouseClickedEvent event)
      Handles click events on the backdrop to close the drawer.
      Specified by:
      onEvent in interface MouseClickListener
      Parameters:
      event - the mouse click event
    • isOpen

      public boolean isOpen()
      Checks if the drawer is currently open.
      Returns:
      true if open, false if closed
    • getPanel

      public Div getPanel()
      Returns the drawer panel for adding content.
      Returns:
      the panel element
    • getBackdrop

      public Div getBackdrop()
      Returns the backdrop element for customization.
      Returns:
      the backdrop element
    • addElement

      public Drawer addElement(Element element)
      Adds content to the drawer panel.
      Overrides:
      addElement in class Element<Drawer>
      Parameters:
      element - the element to add
      Returns:
      this Drawer for method chaining
    • removeAllElements

      public void removeAllElements()
      Removes all content from the drawer panel.
      Overrides:
      removeAllElements in class Element<Drawer>
    • setPanelPadding

      public Drawer setPanelPadding(int padding)
      Sets padding inside the drawer panel.
      Parameters:
      padding - the padding in pixels
      Returns:
      this Drawer for method chaining
    • setPanelPadding

      public Drawer setPanelPadding(String padding)
      Sets padding inside the drawer panel.
      Parameters:
      padding - the padding value (e.g., "1rem", "16px")
      Returns:
      this Drawer for method chaining