Class HashNavComponent


public abstract class HashNavComponent extends Div<HashNavComponent>
Abstract base class for components that synchronize their active element with the browser's URL hash.

HashNavComponent manages a list of elements identified by string IDs and tracks which one is currently active. When the active element changes, the browser's URL hash is updated to match. When the browser's hash changes (via back/forward navigation or direct URL entry), the component automatically activates the matching element.

Subclasses implement onActivated(Element, String, int) and onDeactivated(Element, String, int) to define what "active" and "inactive" mean visually — for example, toggling CSS classes, showing/hiding elements, or switching content panels.

Usage:

 public class SidebarNav extends HashNavComponent
 {
     public SidebarNav()
     {
         addItem("overview", overviewLink);
         addItem("details", detailsLink);
         addItem("settings", settingsLink);
     }

     protected void onActivated(Element element, String id, int index)
     {
         ((StyledElement) element).addClass("active");
     }

     protected void onDeactivated(Element element, String id, int index)
     {
         ((StyledElement) element).removeClass("active");
     }
 }
 
Since:
2026
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • HashNavComponent

      public HashNavComponent()
      Creates a new HashNavComponent.
  • Method Details

    • addItem

      public void addItem(String id, Element element)
      Registers an item with this component.

      The item's ID is used to match against the browser's URL hash fragment. The element is passed to onActivated(com.oorian.html.Element, java.lang.String, int) and onDeactivated(com.oorian.html.Element, java.lang.String, int) when the active state changes.

      Parameters:
      id - The hash identifier for this item (e.g., "overview" maps to #overview in the URL).
      element - The element associated with this item.
    • activate

      public void activate(int index)
      Activates the item at the specified index.

      Deactivates the previously active item (if any), activates the new item, and updates the browser's URL hash to match.

      Parameters:
      index - The zero-based index of the item to activate.
    • activate

      public void activate(String id)
      Activates the item with the specified ID.
      Parameters:
      id - The hash identifier of the item to activate.
    • getActiveIndex

      public int getActiveIndex()
      Returns the index of the currently active item.
      Returns:
      The zero-based index of the active item, or -1 if no item is active.
    • getActiveId

      public String getActiveId()
      Returns the ID of the currently active item.
      Returns:
      The hash identifier of the active item, or null if no item is active.
    • getActiveElement

      public Element getActiveElement()
      Returns the currently active element.
      Returns:
      The active element, or null if no item is active.
    • getItemCount

      public int getItemCount()
      Returns the number of items registered with this component.
      Returns:
      The item count.
    • getItem

      public Element getItem(int index)
      Returns the element at the specified index.
      Parameters:
      index - The zero-based index.
      Returns:
      The element at the index.
    • getItemId

      public String getItemId(int index)
      Returns the ID at the specified index.
      Parameters:
      index - The zero-based index.
      Returns:
      The hash identifier at the index.
    • onActivated

      protected abstract void onActivated(Element element, String id, int index)
      Called when an item becomes active.

      Subclasses implement this to define the visual effect of activation — for example, adding a CSS class, showing the element, or highlighting a nav item.

      Parameters:
      element - The element being activated.
      id - The hash identifier of the item.
      index - The zero-based index of the item.
    • onDeactivated

      protected abstract void onDeactivated(Element element, String id, int index)
      Called when an item becomes inactive.

      Subclasses implement this to define the visual effect of deactivation — for example, removing a CSS class, hiding the element, or unhighlighting a nav item.

      Parameters:
      element - The element being deactivated.
      id - The hash identifier of the item.
      index - The zero-based index of the item.
    • onPageLoaded

      protected void onPageLoaded()
      Description copied from class: Element
      Hook method called when the page has finished loading.

      Override this method to perform actions after the page is fully loaded in the browser.

      Overrides:
      onPageLoaded in class Element<HashNavComponent>
    • onHashChange

      protected void onHashChange(String hash)
      Description copied from class: Element
      Hook method called when the URL hash fragment changes.

      Override this method to respond to hash-based navigation changes within the page.

      Overrides:
      onHashChange in class Element<HashNavComponent>
      Parameters:
      hash - The new hash fragment value.