Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@daviddarnes/code-pen
Advanced tools
code-pen
A Web Component for opening code blocks in CodePen.
Demo | Attributes demo | Further reading
General usage example:
<script type="module" src="code-pen.js"></script>
<code-pen>
<pre>
<code><p>Hello world</p></code>
</pre>
</code-pen>
Note that the code samples have been escaped so they can be seen on the page correctly
Examlpe with 3 code
elements which default to HTML, CSS and JavaScript respectively:
<script type="module" src="code-pen.js"></script>
<code-pen>
<pre>
<code><p>Hello world</p></code>
</pre>
<pre>
<code>:root { color: hotpink; }</code>
</pre>
<pre>
<code>document.querySelector("p").style.backgroundColor = "orange";</code>
</pre>
</code-pen>
This Web Component allows you to:
code
samples in the CodePen editor without any configuration
code
samplecode
samples, in respective ordercode
samples, in respective ordercode
sample is filled into in CodePen using the css
and js
attributes (html
is the default)html
, css
and js
attributes and an element selector as its value (e.g. css="textarea"
)title
attributelabel
attribute contenteditable
on the code containerYou have a few options (choose one of these):
npm install @daviddarnes/code-pen
Make sure you include the <script>
in your project (choose one of these):
<!-- Host yourself -->
<script type="module" src="code-pen.js"></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
type="module"
src="https://www.unpkg.com/@daviddarnes/code-pen@1.2.0/code-pen.js"
></script>
<!-- 3rd party CDN, not recommended for production use -->
<script type="module" src="https://esm.sh/@daviddarnes/code-pen@1.2.0"></script>
By default the <code-pen>
component will assume the first code
element it finds goes into the HTML editor in CodePen, the second goes into the CSS editor, and JavaScript into the third. If there is only one or two code
elements it'll still follow this order and leave the missing ones blank in CodePen. However with attributes the order can be modified and changed.
Applying the css
or js
attributes will cause a single code
elements content to be insered into the CSS or JavaScript editors in CodePen respectively:
<script type="module" src="code-pen.js"></script>
<code-pen css>
<pre>
<code>:root { background: hotpink; }</code>
</pre>
</code-pen>
<script type="module" src="code-pen.js"></script>
<code-pen js>
<pre>
<code>document.body.style.backgroundColor = "orange";</code>
</pre>
</code-pen>
Note that, as mentioned above, not applying any attributes will mean the single code
elements content will be inserted into the HTML editor on CodePen
You can also overwrite the element selection entirely using the html
, css
and js
attributues to set an element selector for each piece of code. This is useful for cases where your code is out of order, you have extra rogue elements in your content or if you wish to use a different element entirely.
<script type="module" src="code-pen.js"></script>
<code-pen html=".language-html" css=".language-css" js=".language-js">
<pre>
<code>I'm a rogue code block to ruin this Web Component demo</code>
</pre>
<pre>
<code class="language-js">document.querySelector("p").style.backgroundColor = "orange";</code>
</pre>
<pre>
<code class="language-html"><p>Hello world</p></code>
</pre>
<pre>
<code class="language-css">:root { color: hotpink; }</code>
</pre>
</code-pen>
<script type="module" src="code-pen.js"></script>
<code-pen css="textarea">
<textarea>:root { background: hotpink; }</textarea>
</code-pen>
Optionally you can set the title of the newly create pen in CodePen as well as the button label for the "Open in CodePen" button using title
and label
respectively:
<script type="module" src="code-pen.js"></script>
<code-pen title="Hello world example" label="Create new pen">
<pre>
<code><p>Hello world</p></code>
</pre>
</code-pen>
The component also works if you want readers to be able to edit the code before opening it in CodePen. Either use a textarea
or input
element to contain the code samples, or add a contenteditable="true"
attribute to the immediate containing element:
<script type="module" src="code-pen.js"></script>
<code-pen>
<pre>
<code contenteditable="true"><p>Hello world</p></code>
</pre>
</code-pen>
The default template for the component looks like this:
<form action="https://codepen.io/pen/define" method="POST" target="_blank">
<input type="hidden" name="data">
<input type="submit" value="Open in CodePen">
</form>
However you can customise the template by using a
FAQs
A Web Component to open code samples in CodePen
The npm package @daviddarnes/code-pen receives a total of 0 weekly downloads. As such, @daviddarnes/code-pen popularity was classified as not popular.
We found that @daviddarnes/code-pen demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.