@hubspot/cms-components is a runtime library providing primitives that interact with CMS React features and other HubSpot data from your components.
Getters
getHubID
( ) => number
Returns the ID of the HubSpot account for the page that’s being rendered.
getIsDeployed
( ) => boolean
Returns true for components rendered live for a deployed project, and false when rendered in the dev server.
getSecret
(secretName: string) => string
Returns a value for a given secret key. The secret must be defined using the hs secrets CLI command and the key must be included in a secretNames array in your cms-assets.json configuration.
To prevent accidentally leaking secrets, getSecret must be called from a React component function that is rendered on the server. In other words, getSecret():
- Cannot be called at the top-level of a module
- Cannot be called from inside an island
Hooks
HubSpot provides the following React hooks to help write components that run on both the server and the browser.useAfterIslandHydration
( ) => boolean
Will return true only after hydration is completed. More specifically it will:
- Return
falseduring the initial render on the server. - Return
falseduring the first render that happens inside the browser. - Return
trueduring any subsequent renders that happen after the component has been “mounted” in the browser.
useIsServerRender
( ) => boolean
Returns true while the component is being rendered on the server and false in the browser. Note that in most cases, it is better to use useAfterIslandHydration(), since it makes it easier for your code to “do the right thing” for hydration.
usePageUrl
( ) => URL
Returns the current page URL. Works on the server and is reactive to changes to the URL on client. This can be useful when components need to react to URL changes, such as query params, while also supporting server rendering. To programmatically trigger non-navigation URL changes, use pushHistoryState() which is identical to window.history.pushState() but integrates with usePageUrl() to ensure it receives change events.
useInlineHeadAsset
(renderFunc: () => JSX.Element) => void
Provides an API to pass HTML to render in the <head>. This is most useful for collecting CSS from CSS-in-JS libraries and including the emitted CSS in the initial page response. See Styling for more details.
useSharedIslandState
useState, but updating the state via updateSharedState(newValue) will update all of the other islands that also use useSharedIslandState(). Works in coordination with the SharedIslandState component.
useSharedIslandReducer
useReducer, but actions dispatched will “reach across” and update all of the other islands that also use useSharedIslandReducer(). Works in coordination with the SharedIslandReducer component.
useMenu
(menuIdentifier: Token) => SiteMenu | null
Fetches and returns menu data for a given menu identifier.
Example
Parameters
Return Type
Returns aSiteMenu object if the menu is successfully loaded, or null if the menu is not found or an error occurs.
SiteMenu:
PageTreeNode:
useEditorVariableChecks
() => Record<string, boolean>
Returns a map of editor variable checks that indicate the current rendering context. These checks help determine if content is being rendered within HubSpot editors or previewers.
Example
Return Type
Returns aRecord<string, boolean> mapping editor variable names to their boolean values. Returns an empty object if the checks are unavailable.
Available Editor Variables:
useBrandSettings
() => BrandSettings | null
Returns the brand settings for the current HubSpot account, including colors, fonts, logos, and favicons configured in the brand settings.
Example
Return Type
Returns aBrandSettings object if brand settings are available, or null if they are not loaded or an error occurs.
BrandSettings:
BrandingLogo:
BrandingFavicon:
Components
Island
Learn more about<Island> in the island documentation.
SharedIslandState
Defines the initial value for the shared state accessed inuseSharedIslandState() by other islands in this JS module or partial. All islands that are “wrapped” by SharedIslandState (i.e. are children or descendents of the children) will share a single state reference.
SharedIslandState must be rendered on the server and cannot be contained inside an island.
SharedIslandReducer
Defines the reducer function and initial value for the shared reducer state accessed inuseSharedIslandReducer() by other islands in this JS module or partial. All islands that are “wrapped” by SharedIslandReducer (i.e. are children or descendants of the children) will share a single state reference and dispatch function.
SharedIslandReducer must be rendered on the server and cannot be contained inside an island. The reducer function passed in must be imported with the ?client suffix (which will automatically prepare code-splitting that function for the browser to grab it).
Field helper components
The following components are designed to be used with associated module field definitions. They cannot be used for non-field related use cases. If an associated field is not found at the providedfieldPath then the components will render null.
Icon
@hubspot/cms-components/Icon
The Icon component renders an SVG for a referenced Icon field.
This component also accepts and applies all valid attributes for an SVG element, such as id and style.
RichText
@hubspot/cms-components/RichText
The RichText component handles inserting the RichText HTML into your component.
This component passes through all valid attributes, such as id and style, to the wrapper tag and applies them.