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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<script src="https://gojs.net/latest/release/go.js"></script>
<div id="sample">
<p>HTML Context menu <b>GoJS</b> Sample</p>
<div style="display: inline-block;">
<!-- We make a div to contain both the Diagram div and the context menu (such that they are siblings)
so that absolute positioning works easily.
This DIV containing both MUST have a non-static CSS position (we use position: relative)
so that our context menu's absolute coordinates work correctly. -->
<div style="position: relative;" >
<div id="myDiagramDiv" style="border: solid 1px black; width:400px; height:400px"></div>
<div id="contextMenu">
<ul>
<li id="cut" onclick="cxcommand(this.textContent)">Cut</li>
<li id="copy" onclick="cxcommand(this.textContent)">Copy</li>
<li id="paste" onclick="cxcommand(this.textContent)">Paste</li>
<li id="delete" onclick="cxcommand(this.textContent)">Delete</li>
<li id="color" onclick="cxcommand('Color')">Color</li>
</ul>
</div>
</div>
<div id="description">
<p>This demonstrates the implementation of a custom HTML context menu.</p>
<p>For a light-box style HTML context menu implementation, see the <a href="htmlLightBoxContextMenu.html">LightBox Context Menu</a> sample.</p>
<p>Right-click or tap-hold on a Node to bring up a context menu.
If you have a selection copied in the clipboard, you can bring up a context menu anywhere to paste.</p>
</div>
</div>
</div>
/* CSS for the traditional context menu */
#contextMenu {
z-index: 300;
position: absolute;
left: 5px;
border: 1px solid #444;
background-color: #F5F5F5;
display: none;
box-shadow: 0 0 10px rgba( 0, 0, 0, .4 );
font-size: 12px;
font-family: sans-serif;
font-weight:bold;
}
#contextMenu ul {
list-style: none;
top: 0;
left: 0;
margin: 0;
padding: 0;
}
#contextMenu li {
position: relative;
min-width: 60px;
color: #444;
display: inline-block;
padding: 6px;
text-decoration: none;
cursor: default;
}
#contextMenu li:hover { background: #444; }
#contextMenu li:hover { color: #EEE; }
var myDiagram = null;
function init() {
if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
var $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
$(go.Diagram, "myDiagramDiv", // create a Diagram for the DIV HTML element
{ initialContentAlignment: go.Spot.Center, "undoManager.isEnabled": true });
// define a simple Node template (but use the default Link template)
myDiagram.nodeTemplate =
$(go.Node, "Auto",
// We make a dummy context menu so that the contextMenuTool will activate,
// but we don't use this adornment
{ contextMenu: $(go.Adornment) },
$(go.Shape, "RoundedRectangle",
// Shape.fill is bound to Node.data.color
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 3 }, // some room around the text
// TextBlock.text is bound to Node.data.key
new go.Binding("text", "key"))
);
// create the model data that will be represented by Nodes and Links
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen" },
{ key: "Delta", color: "pink" }
],
[
{ from: "Alpha", to: "Beta" },
{ from: "Alpha", to: "Gamma" },
{ from: "Beta", to: "Beta" },
{ from: "Gamma", to: "Delta" },
{ from: "Delta", to: "Alpha" }
]);
// This is a dummy context menu for the whole Diagram:
myDiagram.contextMenu = $(go.Adornment);
// Override the ContextMenuTool.showContextMenu and hideContextMenu methods
// in order to modify the HTML appropriately.
var cxTool = myDiagram.toolManager.contextMenuTool;
// This is the actual HTML context menu:
var cxElement = document.getElementById("contextMenu");
// We don't want the div acting as a context menu to have a (browser) context menu!
cxElement.addEventListener("contextmenu", function(e) {
this.focus();
e.preventDefault();
return false;
}, false);
cxElement.addEventListener("blur", function(e) {
cxTool.stopTool();
// maybe start another context menu
if (cxTool.canStart()) {
myDiagram.currentTool = cxTool;
cxTool.doMouseUp();
}
}, false);
cxElement.tabIndex = "1";
// This is the override of ContextMenuTool.showContextMenu:
// This does not not need to call the base method.
cxTool.showContextMenu = function(contextmenu, obj) {
var diagram = this.diagram;
if (diagram === null) return;
// Hide any other existing context menu
if (contextmenu !== this.currentContextMenu) {
this.hideContextMenu();
}
// Show only the relevant buttons given the current state.
var cmd = diagram.commandHandler;
var objExists = obj !== null;
document.getElementById("cut").style.display = objExists && cmd.canCutSelection() ? "block" : "none";
document.getElementById("copy").style.display = objExists && cmd.canCopySelection() ? "block" : "none";
document.getElementById("paste").style.display = cmd.canPasteSelection() ? "block" : "none";
document.getElementById("delete").style.display = objExists && cmd.canDeleteSelection() ? "block" : "none";
document.getElementById("color").style.display = objExists ? "block" : "none";
// Now show the whole context menu element
cxElement.style.display = "block";
// we don't bother overriding positionContextMenu, we just do it here:
var mousePt = diagram.lastInput.viewPoint;
cxElement.style.left = mousePt.x + "px";
cxElement.style.top = mousePt.y + "px";
// Remember that there is now a context menu showing
this.currentContextMenu = contextmenu;
}
// This is the corresponding override of ContextMenuTool.hideContextMenu:
// This does not not need to call the base method.
cxTool.hideContextMenu = function() {
if (this.currentContextMenu === null) return;
cxElement.style.display = "none";
this.currentContextMenu = null;
}
}
// This is the general menu command handler, parameterized by the name of the command.
function cxcommand(val) {
var diagram = myDiagram;
if (!(diagram.currentTool instanceof go.ContextMenuTool)) return;
switch (val) {
case "Cut": diagram.commandHandler.cutSelection(); break;
case "Copy": diagram.commandHandler.copySelection(); break;
case "Paste": diagram.commandHandler.pasteSelection(diagram.lastInput.documentPoint); break;
case "Delete": diagram.commandHandler.deleteSelection(); break;
case "Color": changeColor(diagram); break;
}
diagram.currentTool.stopTool();
}
// A custom command, for changing the color of the selected node(s).
function changeColor(diagram) {
// the object with the context menu, in this case a Node, is accessible as:
var cmObj = diagram.toolManager.contextMenuTool.currentObject;
// but this function operates on all selected Nodes, not just the one at the mouse pointer.
// Always make changes in a transaction, except when initializing the diagram.
diagram.startTransaction("change color");
diagram.selection.each(function(node) {
if (node instanceof go.Node) { // ignore any selected Links and simple Parts
// Examine and modify the data, not the Node directly.
var data = node.data;
if (data.color === "red") {
// Call setDataProperty to support undo/redo as well as
// automatically evaluating any relevant bindings.
diagram.model.setDataProperty(data, "color", go.Brush.randomColor());
} else {
diagram.model.setDataProperty(data, "color", "red");
}
}
});
diagram.commitTransaction("change color");
}
init();
Also see: Tab Triggers