Class AppHeadroom
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasStyle, Serializable
AppLayout's top and bottom navigation bars on scroll.
This component only ever knows about AppLayout's own standard,
public contract — specifically the navbar-top and navbar-bottom
shadow-DOM parts every vaadin-app-layout instance exposes, regardless
of subclass. It has no knowledge of, and no dependency on, any specific
AppLayout extension (e.g. one that adds a persistent side rail):
whatever a subclass does with those two standard parts, AppHeadroom
finds and manages them the same way every time.
By default, a bar that's already pinned to the viewport
(position: fixed) and shaped like a vertical rail rather than a
horizontal bar (taller than wide) is left alone automatically — a plain,
observable geometry fact, not something any extension has to declare. For
cases where that inference isn't right (or an extension's own behavior can't
be reliably inferred that way), setTopBarPinned(boolean) / setBottomBarPinned(boolean) let calling code state it explicitly instead. See those
methods for how this is meant to be wired up from application code.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumCoarse device category, used bysetActivationPredicate(SerializableBiPredicate).static enumViewport orientation, used bysetActivationPredicate(SerializableBiPredicate).static classFired whenever the chrome's pinned/unpinned state changes, whether driven by scrolling on the client or by a server-initiated reset (e.g. detaching this instance viaremove()). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringWire name of thesetBottomBarPinned(boolean)attribute.static final StringWire name of thesetHideTolerance(int)attribute.static final StringWire name of thesetShowTolerance(int)attribute.static final StringWire name of thesetTopBarPinned(boolean)attribute.static final StringWire name of thesetTopOffset(int)attribute.static final StringWire name of thesetTransitionDuration(int)attribute.static final StringWire name of theaddPinnedChangeListener(ComponentEventListener)DOM event.static final StringWire name of theisActive()element property.static final StringWire name of theisPinned()element property. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationaddPinnedChangeListener(com.vaadin.flow.component.ComponentEventListener<AppHeadroom.PinnedChangeEvent> listener) Registers a listener to be notified wheneverisPinned()changes.static AppHeadroomapplyTo(com.vaadin.flow.component.applayout.AppLayout layout) Applies headroom scroll-hide/show behavior to the given AppLayout and returns the component for optional further configuration.booleanisActive()Whether the headroom effect is currently active for this session, per the last evaluation of theactivation predicate.booleanisPinned()Whether the chrome is currently shown (true, the initial and default state) or hidden (false).protected voidonDetach(com.vaadin.flow.component.DetachEvent detachEvent) Resets the server-visible pinned state directly, rather than relying on the client's ownpinned-changedevent during teardown: when detachment is server-initiated (e.g.voidremove()Detaches this instance, removing its scroll-hide/show behavior from theAppLayoutit was applied to.setActivationPredicate(com.vaadin.flow.function.SerializableBiPredicate<AppHeadroom.DeviceType, AppHeadroom.Orientation> predicate) Sets the predicate that decides whether the headroom effect should be active, evaluated against this session's detectedAppHeadroom.DeviceTypeand currentAppHeadroom.Orientation.setBottomBarPinned(boolean pinned) Same assetTopBarPinned(boolean), for the bottom bar.setHideTolerance(int px) Sets how far, in pixels, the user must scroll down past the last shown-position high-water mark before the chrome hides (default30).setShowTolerance(int px) Sets how far, in pixels, the user must scroll up past the last hidden-position low-water mark before the chrome is restored (default30).setTabletMinShortSidePx(int px) Overrides the physical-screen-shorter-side threshold, in CSS pixels, used to distinguishAppHeadroom.DeviceType.TABLETfromAppHeadroom.DeviceType.PHONEamong touch devices (default768, matchingvaadin-flow-app-nav-layout's equivalent device detection).setTopBarPinned(boolean pinned) Explicitly overrides whether the top bar is treated as pinned (never hidden), regardless of the automatic position/shape inference.setTopOffset(int px) Sets the distance from the top of the page, in pixels, within which the chrome is always shown (default100).setTransitionDuration(int ms) Sets how long, in milliseconds, the show/hide slide and padding transitions take (default600).Methods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisibleMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Field Details
-
ATTR_TOP_OFFSET
-
ATTR_HIDE_TOLERANCE
Wire name of thesetHideTolerance(int)attribute.- See Also:
-
ATTR_SHOW_TOLERANCE
Wire name of thesetShowTolerance(int)attribute.- See Also:
-
ATTR_TOP_BAR_PINNED
Wire name of thesetTopBarPinned(boolean)attribute.- See Also:
-
ATTR_BOTTOM_BAR_PINNED
Wire name of thesetBottomBarPinned(boolean)attribute.- See Also:
-
PROPERTY_PINNED
-
PROPERTY_ACTIVE
-
EVENT_PINNED_CHANGED
Wire name of theaddPinnedChangeListener(ComponentEventListener)DOM event.- See Also:
-
ATTR_TRANSITION_DURATION
Wire name of thesetTransitionDuration(int)attribute.- See Also:
-
-
Method Details
-
applyTo
Applies headroom scroll-hide/show behavior to the given AppLayout and returns the component for optional further configuration.The returned instance attaches itself as a peer under the current
UI's root element, never as a light-DOM child oflayout— so it never shows up inlayout.getChildren(). This happens automatically wheneverlayoutitself is attached (immediately, if it already is), and followslayoutacross any subsequent detach/re-attach. Callremove()to detach headroom behavior fromlayoutwithout affecting the layout itself.The effect is active on every device by default; see
setActivationPredicate(SerializableBiPredicate)to restrict it by device type/orientation. -
remove
public void remove()Detaches this instance, removing its scroll-hide/show behavior from theAppLayoutit was applied to. Has no effect if already detached. -
setTopOffset
Sets the distance from the top of the page, in pixels, within which the chrome is always shown (default100). Returnsthisfor chaining.- Throws:
IllegalArgumentException- ifpxis negative
-
setHideTolerance
Sets how far, in pixels, the user must scroll down past the last shown-position high-water mark before the chrome hides (default30). Returnsthisfor chaining.- Throws:
IllegalArgumentException- ifpxis negative
-
setShowTolerance
Sets how far, in pixels, the user must scroll up past the last hidden-position low-water mark before the chrome is restored (default30). Returnsthisfor chaining.- Throws:
IllegalArgumentException- ifpxis negative
-
setTransitionDuration
Sets how long, in milliseconds, the show/hide slide and padding transitions take (default600). Returnsthisfor chaining.- Throws:
IllegalArgumentException- ifmsis negative
-
setTopBarPinned
Explicitly overrides whether the top bar is treated as pinned (never hidden), regardless of the automatic position/shape inference.Not called directly by any
AppLayoutextension — extensions have no reason to knowAppHeadroomexists. Instead, this is meant to be wired up from application code that already explicitly combines anAppLayoutextension withAppHeadroom(for example, code overriding an extension's own hook for observing layout-mode changes, if it has one), reacting to whatever that extension's own public API exposes about its current state and calling this method accordingly. -
setBottomBarPinned
Same assetTopBarPinned(boolean), for the bottom bar. -
isPinned
@Synchronize("pinned-changed") public boolean isPinned()Whether the chrome is currently shown (true, the initial and default state) or hidden (false). Kept in sync with the client's own scroll-driven pin/unpin state; seeaddPinnedChangeListener(ComponentEventListener)to be notified of changes instead of polling this. -
addPinnedChangeListener
public com.vaadin.flow.shared.Registration addPinnedChangeListener(com.vaadin.flow.component.ComponentEventListener<AppHeadroom.PinnedChangeEvent> listener) Registers a listener to be notified wheneverisPinned()changes. -
onDetach
protected void onDetach(com.vaadin.flow.component.DetachEvent detachEvent) Resets the server-visible pinned state directly, rather than relying on the client's ownpinned-changedevent during teardown: when detachment is server-initiated (e.g.layout.remove(headroom)), Flow stops routing further client events for this component the moment removal begins, so the client-fired event fromdisconnectedCallback()never reaches here.- Overrides:
onDetachin classcom.vaadin.flow.component.Component
-
setActivationPredicate
public AppHeadroom setActivationPredicate(com.vaadin.flow.function.SerializableBiPredicate<AppHeadroom.DeviceType, AppHeadroom.Orientation> predicate) Sets the predicate that decides whether the headroom effect should be active, evaluated against this session's detectedAppHeadroom.DeviceTypeand currentAppHeadroom.Orientation. Re-evaluated automatically whenever either becomes known or changes (e.g. on device rotation) — there's nothing further to wire up.The default predicate always returns
true: the effect is active on every device unless this is called. There's no separate plain on/off switch — force it fully on or off yourself with(deviceType, orientation) -> true/-> false, or restrict it by device, e.g. to restore the original phone-always/tablet-landscape-only behavior this had before being extracted into this library:AppHeadroom.applyTo(layout).setActivationPredicate((deviceType, orientation) -> deviceType == AppHeadroom.DeviceType.PHONE || (deviceType == AppHeadroom.DeviceType.TABLET && orientation == AppHeadroom.Orientation.LANDSCAPE));- Returns:
- this, for chaining
-
setTabletMinShortSidePx
Overrides the physical-screen-shorter-side threshold, in CSS pixels, used to distinguishAppHeadroom.DeviceType.TABLETfromAppHeadroom.DeviceType.PHONEamong touch devices (default768, matchingvaadin-flow-app-nav-layout's equivalent device detection).Must be called before the target
AppLayoutattaches to take effect — device type is detected once, at first attach, same assetActivationPredicate(SerializableBiPredicate)'s predicate is evaluated against whatever's known at the time.- Returns:
- this, for chaining
- Throws:
IllegalArgumentException- ifpxis negative
-
isActive
public boolean isActive()Whether the headroom effect is currently active for this session, per the last evaluation of theactivation predicate. Defaults totrueuntil device type and orientation are both known (shortly after the targetAppLayoutattaches).
-