How to make sure your CSS/JS displays well in the HubSpot CMS editors.
event.stopPropagation()
, the event will not bubble up to the document
or window
. If there are event listeners on those elements, the code in those listeners will not run. The issue with the above code is that the click handler will run on every click, which causes problems in the inline editor because it adds click handlers to the window element via React. Instead of listening for every click, it would be more appropriate to only add the click handler when needed — for example, after a user has clicked a button to open a side menu — and then removing the handler once the click has fired.
label
. When possible, you should instead use selectors that are specific to a portion of the webpage, such as .hs-form label
.
Being more specific with CSS selectors allows you to pinpoint the elements you want to style without impacting other elements unintentionally. You can also take advantage of our boilerplate CSS file to have a better sense of selectors that should be used to avoid CSS bleed issues.
!important
tags are used to make styling rules take precedence over others. While you can use an !important
tag when styling is being overridden, it’s not recommended to use this tag on bare element selectors like label
or input[type="text"]
.
For example, you might consider applying an !important
tag for styling the <label>
element, with the intent to ensure that all <label>
elements in a form are white.
<label>
tags in the content editor as white, as shown below.
<html>
element within this iframe is assigned a class of .hs-inline-edit
. Using this class, you can write CSS that conditionally renders based on the presence of that iframe.
For example, the following CSS takes advantage of the pseudo-class :not()
so that the rule does not apply when loaded in the editor:
.hs-inline-edit class
, the new rule will only apply in the editor, not the live page. For reference, the rich text toolbar’s z-index is 2147483647
.
window.hsInEditor
will return true
. You can include this variable in your JavaScript to conditionally run code based on whether the content is within the context of the editor. This can be useful when JavaScript is negatively impacting the in-app editing experience.
For example, you might find that your JavaScript isn’t working as expected because it’s running before the page editor has loaded. If your site uses jQuery, you could use the document ready handler to run the code only once the page editor has loaded fully.
true
in various editor and preview contexts. This includes variables that can check for any editor or preview context, as well as specific editor and preview contexts.
For example, the if statement below would render its content only when the user is in the blog post editor.