HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="stencil-container"></div>
<div id="paper-container"></div>
<div id="inspector-container"></div>
<div id="toolbar-container"></div>
<a target="_blank" href="https://www.jointjs.com">
<img id="logo" src="https://assets.codepen.io/7589991/jj-logo-red.svg" width="200" height="50"></img>
</a>
/* Paper */
#paper-container {
position: absolute;
right: 200px;
top: 0;
left: 100px;
bottom: 0;
.selection-wrapper[data-selection-length="1"] {
display: block;
~ .selection-box {
display: none;
}
}
}
/* Stencil */
#stencil-container {
position: absolute;
left: 0;
top: 0;
width: 100px;
bottom: 0;
z-index: 100;
.joint-stencil {
box-shadow: 0 0 5px 5px #cbd4da;
border-right: 1px solid #b5c0c9;
}
.joint-cell * {
cursor: grab;
}
}
/* Inspector */
#inspector-container {
position: absolute;
right: 0;
top: 0;
width: 200px;
bottom: 0;
box-shadow: 0 0 5px 5px #cbd4da;
border-left: 1px solid #b5c0c9;
.joint-inspector {
background-color: #f2f5f8;
label {
font-size: 14px;
color: #131e29;
}
}
.select-box-option-content {
border: 1px solid #b5c0c9;
}
::placeholder {
font-weight: bold;
color: #9e9e9e;
}
}
/* Hide Multiple Values color icon */
.select-box-option:first-child {
display: none;
}
/* Toolbar */
#toolbar-container {
position: absolute;
top: 15px;
right: 215px;
border-radius: 5px;
padding: 5px;
background-color: white;
display: flex;
flex-direction: column;
align-items: center;
color: #131e29;
font-family: sans-serif;
border: 1px solid #eaf0f5;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
.joint-toolbar {
border: none;
padding: 0;
justify-content: space-around;
button {
border-color: #d6e1eb;
text-transform: capitalize;
cursor: default;
&:not([disabled]) {
color: #131e29;
&:hover {
background: #e7eef3;
cursor: pointer;
}
}
}
}
}
#logo {
position: absolute;
bottom: 20px;
right: 0;
}
const { dia, ui, shapes, linkTools } = joint;
const colors = {
shapeStroke: "#ed2637",
shapeFill: "#f6f6f6",
text: "#131e29",
bg: "#dde6e9",
stencilBg: "#f2f5f8"
};
// Initialize graph and paper
const graph = new dia.Graph({}, { cellNamespace: shapes });
const paper = new dia.Paper({
model: graph,
cellViewNamespace: shapes,
width: "100%",
height: "100%",
gridSize: 20,
drawGrid: { name: "mesh" },
async: true,
sorting: dia.Paper.sorting.APPROX,
background: { color: colors.bg },
restrictTranslate: true,
clickThreshold: 10
});
document.getElementById("paper-container").appendChild(paper.el);
// Initialize stencil (element palette)
const stencil = new ui.Stencil({
paper,
usePaperGrid: true,
width: 100,
height: "100%",
paperOptions: () => {
return {
model: new dia.Graph({}, { cellNamespace: shapes }),
cellViewNamespace: shapes,
background: {
color: colors.stencilBg
}
};
},
layout: {
columns: 1,
rowHeight: "compact",
rowGap: 10,
columnWidth: 100,
marginY: 10,
// reset defaults
resizeToFit: false,
dx: 0,
dy: 0
}
});
stencil.render();
document.getElementById("stencil-container").appendChild(stencil.el);
// Add items to stencil
stencil.load([
{
type: "standard.Rectangle",
size: { width: 80, height: 60 },
attrs: {
label: {
text: "",
fill: colors.text,
fontFamily: "sans-serif"
},
body: {
rx: 0,
stroke: colors.shapeStroke,
fill: colors.shapeFill
}
}
},
{
type: "standard.Ellipse",
size: { width: 80, height: 60 },
attrs: {
label: {
text: "",
fill: colors.text,
fontFamily: "sans-serif"
},
body: {
stroke: colors.shapeStroke,
fill: colors.shapeFill
}
}
}
]);
// Initialize selection
const selection = new ui.Selection({
paper,
useModelGeometry: true,
theme: "material",
boxContent: false
});
selection.removeHandle("resize");
selection.removeHandle("rotate");
// Set up JointJS interactions
paper.on("blank:pointerdown", function (evt, x, y) {
// Drag on an empty area of the paper to initiate selection lasso
selection.startSelecting(evt);
});
paper.on("element:pointerclick", function (elementView) {
// Click an element to select it
selection.collection.reset([elementView.model]);
});
stencil.on("element:drop", function (elementView) {
// Select dropped element
selection.collection.reset([elementView.model]);
});
selection.collection.on("reset", function () {
// Whenever the selection is changed, refresh inspector
openInspector();
});
function openInspector() {
// Close inspector, if it was previously open
ui.Inspector.close();
// If nothing is selected, do nothing
const cells = selection.collection.models;
if (cells.length === 0) return;
// else: Something is selected - open inspector
function getTextDefinition(path, label) {
// Among all selected cells, get the first value of property at given `path`
const [value, ...otherValues] = cells.map((cell) => cell.prop(path));
// If more than one cell is selected, do they all have the same value for this property?
const hasMultipleValues = otherValues.some(
(otherValue) => value !== otherValue
);
// Return information for setting up the inspector field:
// - If there are multiple values, put "Multiple Values" as placeholder (but not as `value`) of the inspector field
// - If there is only one value, set it as `value` of the inspector field
return {
value: hasMultipleValues ? "" : value || "",
inspector: {
type: "text",
label,
attrs: {
input: {
placeholder: hasMultipleValues ? "⸻" : null
}
}
}
};
}
function getNumberDefinition(path, label) {
// Among all selected cells, get the first value of property at given `path`
const [value, ...otherValues] = cells.map((cell) => cell.prop(path));
// If more than one cell is selected, do they all have the same value for this property?
const hasMultipleValues = otherValues.some(
(otherValue) => value !== otherValue
);
// Return information for setting up the inspector field:
// - If there are multiple values, put "Multiple Values" as placeholder (but not as `value`) of the inspector field
// - If there is only one value, set it as `value` of the inspector field
return {
value: hasMultipleValues ? "" : value || 0,
inspector: {
type: "number",
label,
min: 0,
max: 20,
attrs: {
input: {
placeholder: hasMultipleValues ? "⸻" : null
}
}
}
};
}
function getColorDefinition(path, label) {
// Among all selected cells, get the first value of property at given `path`
const [value, ...otherValues] = cells.map((cell) => cell.prop(path));
// If more than one cell is selected, do they all have the same value for this property?
const hasMultipleValues = otherValues.some(
(otherValue) => value !== otherValue
);
// Return information for setting up the inspector field:
// - If there are multiple values, set `undefined` as `value` of the inspector field - this chooses the `undefined` icon
// - If there is only one value, set it as `value` of the inspector field - this chooses the appropriate color among `options`
return {
value: hasMultipleValues ? undefined : value.toLowerCase(),
inspector: {
type: "color-palette",
options: [
{
content: undefined,
icon: "https://assets.codepen.io/7589991/multiple-values.svg"
},
{ content: "#f6f6f6" }, // colors.shapeFill
{ content: "#dcd7d7" },
{ content: "#8f8f8f" },
{ content: "#c6c7e2" },
{ content: "#feb663" },
{ content: "#fe854f" },
{ content: "#ed2637" }, // colors.shapeStroke
{ content: "#b75d32" },
{ content: "#31d0c6" },
{ content: "#7c68fc" },
{ content: "#61549c" },
{ content: "#6a6c8a" },
{ content: "#4b4a67" },
{ content: "#3c4260" },
{ content: "#33334e" },
{ content: "#222138" }
],
label
}
};
}
// Which properties do all available element shapes (rectangle and ellipse) have in common?
const labelTextPath = "attrs/label/text";
const bodyFillPath = "attrs/body/fill";
const bodyStrokePath = "attrs/body/stroke";
// - Get information for setting up the common inspector fields:
const labelTextDef = getTextDefinition(labelTextPath, "Label");
const bodyFillDef = getColorDefinition(bodyFillPath, "Fill Color");
const bodyStrokeDef = getColorDefinition(bodyStrokePath, "Outline Color");
// - Initialize a synthetic cell object to hold the value of all properties shared among selected cells
const group = new dia.Cell();
// - Add common property values to the shared cell object
group.prop(labelTextPath, labelTextDef.value);
group.prop(bodyFillPath, bodyFillDef.value);
group.prop(bodyStrokePath, bodyStrokeDef.value);
// - Initialize an object to hold the inspector setup for each property field, and add common property field definitions to it
const inputs = {
[labelTextPath]: labelTextDef.inspector,
[bodyFillPath]: bodyFillDef.inspector,
[bodyStrokePath]: bodyStrokeDef.inspector
};
// Additional shape-specific properties:
// - Used if all selected cells are of the same shape type
if (cells.every((cell) => cell.get("type") === "standard.Rectangle")) {
// Rectangle-specific properties:
const bodyRxPath = "attrs/body/rx";
// - Get information for setting up the inspector field
const bodyRxDef = getNumberDefinition(bodyRxPath, "Corner Radius");
// - Add property value to the shared cell object
group.prop(bodyRxPath, bodyRxDef.value);
// - Add property field definition to the inspector setup object
inputs[bodyRxPath] = bodyRxDef.inspector;
}
if (cells.every((cell) => cell.get("type") === "standard.Ellipse")) {
// Ellipse-specific properties:
// (none)
}
// If a property within the shared cell object changes (i.e. when the user edits the field in the inspector), change the property for all selected cells
group.on("change", (cell, opt) => {
if (!opt.inspector) return;
// Use batch to ensure that all changes are done together
// - They will be done together for undo/redo too
graph.startBatch("inspector-bulk-change");
cells.forEach((cell) =>
cell.prop(opt.propertyPathArray, opt.propertyValue, opt)
);
graph.stopBatch("inspector-bulk-change");
});
// Create the inspector based on the shared cell object and the inspector setup object
// - `cell` = the underlying data (`value` in above code)
// - `inputs` = Which parts of the underlying data are editable? Which input field represents each of them? What label does each of them have? (`inspector` in above code)
ui.Inspector.create("#inspector-container", {
cell: group,
theme: "material",
inputs
});
}
// Initialize command manager (undo/redo)
const history = new dia.CommandManager({
graph
});
history.on("stack:undo stack:redo", function () {
// Make sure to deselect removed elements
selection.collection.reset(
selection.collection.filter((cell) => graph.getCell(cell))
);
});
// Initialize toolbar (to hold undo/redo UI buttons)
const toolbar = new joint.ui.Toolbar({
autoToggle: true,
tools: ["undo", "redo"],
references: {
commandManager: history
}
});
document.getElementById("toolbar-container").appendChild(toolbar.render().el);
// Initialize sample diagram content (also added to history)
graph.addCells([
{
type: "standard.Rectangle",
position: { x: 200, y: 200 },
size: { width: 80, height: 60 },
attrs: {
label: {
text: "A",
fill: colors.text,
fontFamily: "sans-serif"
},
body: {
rx: 0,
stroke: colors.shapeStroke,
fill: colors.shapeFill
}
}
},
{
type: "standard.Ellipse",
position: { x: 300, y: 300 },
size: { width: 80, height: 60 },
attrs: {
label: {
text: "B",
fill: colors.text,
fontFamily: "sans-serif"
},
body: {
stroke: colors.shapeStroke,
fill: "#dcd7d7"
}
}
}
]);
// Select all initial sample elements
selection.collection.reset(graph.getElements());
Also see: Tab Triggers