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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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.
<h1>React -from- Scratch</h1>
<div id="container">
<footer>
<h3>What is this... "from scratch" thing?</h3>
<p>This is a small project I try to write out from memory when possible.
The Task: Write out all non-commented code in the JS code section from memory:</p>
<p>The "enhanced" version of this project is at: <a href="https://codepen.io/KeithDC/pen/dzdqaV">https://codepen.io/KeithDC/pen/dzdqaV</a>
(it has TypeScript.)</p>
<hr/>
<div id="last-time">
My last time entry from <a href="https://codepen.io/KeithDC/pen/dVrwrg">my working version</a> <i>(which includes; Palindrome, FizzBuzz, Pomodoro, and GistBox (GetGist))</i>
<pre>// 2018-03-26 (Mon) // Did all 4 (40 min)
// Started: 6:30 PM
// Finished: 7:10 PM</pre>
</div>
<hr/>
<cite>
This project was inspired by Laracasts' <a href="https://laracasts.com/series/do-you-react/episodes/7">"Do You React?" [episode 7]</a> (early 2017; although the code has been cleaned and kept up to at least Dec 2017)
</cite>
<code class="hidden"><cite>
Times: [
{'2017-04-17', '(couldn't complete')'<span class="hidden">, comments: '11:20 - '</span>},
{'2017-04-18', '1.25 hrs'<span class="hidden">, comments: '10:40 -> 11:55 -- Got stuck on xhr - confused xhr.readyState with xhr.status. [readyState] is state of XHR request (waiting for DONE; 4), while [status] is HTTP response status (404, 304, 200).'</span>},
{'2017-04-19', '1.50 hrs'<span class="hidden">, comments: '1:45pm -> 3:15pm -> 3:40pm -- Got stuck on adding new gist into gistList array. Brought over into sublime and figured out 2 issues with gistList loop (tried forEach, but needs map for the return; couldn't use {} in arrow function).'</span>},
{'2017-04-20', '~25 min'<span class="hidden">, comments: '2:28pm -> 2:47pm -> 2:51pm -- Error #1: 'map' from undefined. Inital state was set as 'GistList'. Error #2: username not showing and url incorrect. Forgot to extrapolate the proper data from [gist.owner.login] and [gist.html_url]'</span>},
{'2017-04-21', '~20 min'<span class="hidden">, comments: '11:18am -> 11:37am -- As with yesterday, had to use Dev Tools to figure out a couple hiccups.'</span>},
{'2017-04-22', '~45 min'<span class="hidden">, comments: '2:48pm -> 3:18pm -- First time trying to write the new fetch() format in lieu of XMLHttpRequest. Problem came when trying to figure out why footer contents weren't showing up. I'd forgotten I'm now making a call to props.children to show that content. 3:38pm - Now works with new fetch()'</span>},
{'2017-04-23', '18 min'<span class="hidden">, comments: '6:29pm -> 6:47pm -- Wrote with fetch() method in lieu of XMLHttpRequest.'</span>},
{'2017-04-24', '20 min'<span class="hidden">, comments: '1:38 pm -> 1:57 pm -- works; 1:58 pm - added 'User has no gists' check and alert == 20 min - Got caught up for a few minutes because I didn't wrap my GistBox, GistList, nor my Gist return expressions in <div> tags. This is why I do this once daily.'</span>},
{'2017-04-25', '18 min'<span class="hidden">, comments: '11:58pm -> 12:16am'</span>},
{'2017-04-28', '17 min'<span class="hidden">, comments: '12:29pm -> 12:46pm -- Drew a blank on: response.json() -> went with .getJson() -> solved with a console.log on response in first .fetch() .then()'</span>},
{'2017-04-29', '16 min'<span class="hidden">, comments: '11:36am -> 11:52am -- A few blunders (Dev Tools console to the rescue); 1: mispelled 'proops' in constructor param - 2: Forgot <form> for submit - 3: User onClick instead of onChange - 4: Forgot 'this.' when calling updateUser with last gist - 5: Didn't add 'no gists' check -- Suggestion: Take it slower and be more mindful'</span>},
{'2017-04-30', '16 min'<span class="hidden">, comments: '6:16pm -> 6:32pm -- Worked; No assistance needed from dev tools'</span>},
{'2017-05-01', '12 min'<span class="hidden">, comments: '1:26pm -> 1:38pm -- Worked; One console.log output showed I missed wrapping the form for submit'</span>},
{'2017-05-03', '12 min'<span class="hidden">, comments: '4:28pm -> 4:40pm -- Worked; Although CodePen choked up when I pasted in the <footer> section'</span>},
{'2017-05-15', '32 min'<span class="hidden">, comments: '5:36pm -> 5:55pm -> 6:08pm -- Mostly just forgot syntax. Also forgot the fetch `response.json()`'</span>},
{'2017-05-16', '13 min'<span class="hidden">, comments: '6:26pm - 6:39pm -> 1 small miss - form input didn't clear (made input value [username] not [user])'</span>},
{'2017-05-17', '15 min'<span class="hidden">, comments: '6:29pm -> 6:39pm -> 6:44pm -- Forgot ReactDOM.render, and had to track down misspelled 'gettUser' prop callback.'</span>},
{'2017-05-23', '21 min'<span class="hidden">, comments: '7:14pm -> 7:28pm -> 7:35pm -- Missed ending } on form element; Missed renaming Gist to GistBox (copy/pasta); Mis-typed 'gist.length' instead of 'data.length' (from when I moved the if condition out of 'updateGistList' method)'</span>},
{'2017-07-04', '22 min'<span class="hidden">, comments: '5:22pm -> 5:37pm -> 5:44pm -- Missed 'onSubmit' from 'Form onSubmit={this.submitUser}'; Missed 'return' from 'return response.json()'; Missed specifying parent 'gist' from 'gist.html_url'; Missed wrapping {braces} from this.setState({gistList}); When last issue was fixed, didn't even realize the "t" (username) was being displayed. Doh!; But I did do the entire fetch.'</span>},
{'2017-08-18', '23 min'<span class="hidden">, comments: '1:49pm -> 2:12pm'</span>},
{'2017-10-22', 'Training continued at: https://codepen.io/KeithDC/pen/dVrwrg'}
]
</cite></code>
</footer>
<!--
DISPLAY
FORM
- Input - Text Field
- Button - Submit ('Get Gist')
LIST
- Gists
METHODS
Input - handleUpdate (local component state)
Button - handleSubmit (container callback; validate form, API call, post to source state)
DATA
Input (from user)
- gist username
- use username to get user gists
Output (source state)
[{
- owner.login
- html_url
}]
-->
</div>
body {
font-family: arial;
font-size: 14px;
}
.listitem {
font-size: 12px;
}
footer {
font-size: 11px;
position: fixed;
bottom: 0;
left: 0;
right: 0;
}
cite {
display: block;
font-size: 10px;
margin-bottom: .2rem;
}
.hide,
.hidden {
display: none;
}
// daily-project: GetGist
// This is a 'from scratch' project I try to write out when possible
// Note: I don't write out the 'dangerouslySetInnerHTML' part.
// My new updated version is at: https://codepen.io/KeithDC/pen/dzdqaV
// Although I would focus on this one first (there's no TypeScript).
// This project was inspired by Laracasts (early 2017):
// https://laracasts.com/series/do-you-react/episodes/7
// Your Task: Write out everything below from memory:
// --------------------------------------------------
// GistBox
// GistForm
// GistList
// Gist
;console.clear();
const noGistMsg = 'This GitHub user has no Gists.';
class GistBox extends React.Component {
state = {
gistList: []
};
getGist = (usernameInput) => {
const url = `https://api.github.com/users/${usernameInput}/gists`;
async function fetchAsync(getUrl) {
let response = await fetch(getUrl, { method: 'GET' });
let data = await response.json();
return data;
};
fetchAsync(url).then( data => {
var thisGist = {};
if (data.length > 0) {
thisGist = data[0];
} else {
thisGist = {
owner: {
login: usernameInput
},
html_url: noGistMsg
};
}
this.updateGistList(thisGist);
})
}
updateGistList = (gist) => {
let username = gist.owner.login,
url = gist.html_url,
gistList = this.state.gistList;
gistList = gistList.concat({ username, url });
this.setState({ gistList });
}
render() {
return (
<div>
<GistForm submitUser={this.getGist} />
<GistList gistList={this.state.gistList} />
{this.props.children}
</div>
);
}
}
class GistForm extends React.Component {
state = {
username: ''
};
formChange = (e) => {
this.setState({ username: e.target.value });
}
formSubmit = (e) => {
e.preventDefault();
if (this.state.username.length > 0) {
this.props.submitUser(this.state.username);
this.setState({ username: '' });
}
}
render() {
return (
<div>
<form onSubmit={this.formSubmit}>
<label>Enter a GitHub username for a link to their last Gist:<br/>
<input
type="text"
onChange={this.formChange}
value={this.state.username}
placeholder="kdcinfo"
autoFocus={true}
/>
</label>
<button>Get Gist</button>
</form>
</div>
);
}
}
class GistList extends React.Component {
render() {
const { gistList } = this.props;
return (
<table>
<tbody>
{ gistList.map( (gist, idx) => <Gist key={idx} gist={gist} /> ) }
</tbody>
</table>
);
}
}
class Gist extends React.Component {
render() {
const { username, url } = this.props.gist,
userurl = (url === noGistMsg) ? noGistMsg : <a href={url} target="kdcNewWin">{url}</a>;
return (
<tr className="listitem">
<td>{username}</td>
<td>{userurl}</td>
</tr>
);
}
}
ReactDOM.render(
<GistBox>
<footer dangerouslySetInnerHTML={{__html: document.querySelector('footer').innerHTML}}></footer>
</GistBox>,
container
)
Also see: Tab Triggers