Visible
Overview
The VisibleElement and VisibleClose components are part of a UI system that controls the visibility and transition effects of elements in a React application. These components make it easy to manage elements that can be shown, hidden, and transitioned with specific styles and behaviors.
VisibleElement
The VisibleElement component wraps any content you want to show or hide based on the visibility state. It supports visibility management with transitions, custom styles, and context for child components.
Props
The component is an iplementation for a div element, meaning it inherits all the properties of a div along with the additional props specified in the table below.
elementId
string
A unique identifier for the element. Used to manage visibility for multiple elements.
initialValue
boolean | "DISPLAY_ON_DEVELOPMENT"
Initial visibility state. If set to "DISPLAY_ON_DEVELOPMENT", it will only be visible in development mode.
closedStyles
React.CSSProperties
Inline styles to apply when the element is closed (hidden).
closedClassName
string
CSS class name to apply when the element is closed.
closingStyles
React.CSSProperties
Inline styles to apply durning the closing transition.
closingClassName
string
CSS class name to apply durning the closing transition.
onOpen
() ⇒ void
Callback to be called when the element is opened (becomes visible).
onClose
() ⇒ void
Callback to be called when the element is closed (becomes invisible).
Example
How It Works
The
VisibleElementcomponent listens for visibility updates using theuseVisiblehook.The visibility state (
visible) is dynamically managed, and CSS transitions are applied for opening and closing animations.The
VisibleContextprovides visibility state and actions (close,isClosing) to child components, likeVisibleButton, so they can interact with the element's visibility.The element's styles and class names are conditionally applied based on the visibility and closing state to create smooth transitions.
VisibleClose
The VisibleClose component is a button that interacts with the visibility of a VisibleElement. It allows users to close the associated element when clicked. This component is tightly coupled with the VisibleContext provided by the VisibleElement.
Props
The component is a forwardRef implementation for a button element, meaning it inherits all the properties of a button
Example
The example from VisibleElement
IMPORTANT: <VisibleClose /> must be placed inside <VisibleElement />
How It Works
The
VisibleCloseuses theVisibleContextto access the visibility state of the associatedVisibleElement.It triggers the
closefunction from the context when clicked, which will hide the associatedVisibleElement.The button is disabled during the closing transition (
isClosing), ensuring no further interactions can happen until the transition completes.
Conclusion
The
VisibleElementcomponent provides a flexible way to manage visibility with transition effects, and it uses context to enable interaction with child components.The
VisibleClosecomponent acts as an interactive control for theVisibleElement, allowing the user to close the element.
These components are great for building dynamic UIs where elements need to be shown, hidden, and transitioned based on user actions.
Last updated