JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<div class="rick-and-morty"></div>
/* Base Styles */
* {
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
body {
font-family: 'Source Sans Pro', sans-serif;
font-weight: 400;
color: #fff;
min-height: 100vh;
padding: 20px;
text-align: center;
text-shadow: 1px 1px 1px #111;
background: linear-gradient(rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.8)),
#111 url(https://source.unsplash.com/RF4p4rTM-2s/1280x720) no-repeat center fixed;
background-size: cover;
}
:focus {
outline: 3px solid #02b1c8;
}
/* Heading */
.heading {
font-family: 'Schoolbell', serif;
font-size: 100px;
line-height: 1.0;
color: #02b1c8;
margin: 20px 0 25px;
text-shadow: 0.02em 0.02em 0.02em #7ffa67,
-0.02em -0.02em 0.02em #7ffa67,
0 0 0.2em #7ffa67;
animation: textShadow 2s linear infinite alternate;
}
.heading span {
font-size: 50px;
margin: 0 -12px;
vertical-align: middle;
}
/* Search */
.search-loader {
width: 50px;
height: 50px;
border: 4px solid #fff;
border-top-color: transparent;
border-radius: 50%;
margin: 0 auto;
animation: spin 500ms linear infinite;
}
.search-input-label {
display: block;
font-size: 25px;
}
.search-input {
font-family: inherit;
font-size: 20px;
max-width: 100%;
width: 280px;
height: 35px;
border: none;
border-radius: 15px;
margin: 10px 0 30px;
text-align: center;
color: #111;
background-color: #e5e5e5;
box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.3);
}
.search-input:focus {
outline: none;
box-shadow: 0 0 0 3px #02b1c8;
}
.search-output {
max-width: 750px;
margin: 0 auto 20px;
}
/* Characters */
.character-details {
border-bottom: 2px solid #151515;
}
.character-summary {
font-size: 28px;
font-weight: 600;
line-height: 1.2;
text-align: left;
padding: 20px;
background-color: #222;
cursor: pointer;
}
.character-container {
display: flex;
line-height: 1.2;
padding: 25px;
background-color: #333;
border-top: 2px solid #151515;
}
.character-info {
flex: 1;
margin-right: 25px;
}
.character-info-item-summary {
font-size: 25px;
font-weight: 400;
color: #00e1ff;
text-align: center;
margin-bottom: 10px;
cursor: pointer;
}
.character-info-item-data {
font-size: 22px;
margin-bottom: 10px;
}
.character-image-container {
flex: 1;
display: flex;
}
.character-image {
max-width: 100%;
margin: auto;
border: 5px solid #fcfcfc;
border-radius: 20px;
}
.no-results {
font-size: 30px;
}
/* Page Navigation */
.page-btn {
display: inline-block;
font: inherit;
font-size: 22px;
font-weight: 600;
background: none;
border: none;
color: inherit;
padding: 10px 20px;
margin: 10px;
cursor: pointer;
background-color: #222;
box-shadow: 0 0 0 3px #333;
}
/* Media Queries */
@media (max-width: 600px) {
.character-container {
flex-direction: column;
}
.character-info {
order: 1;
margin-top: 25px;
margin-right: 0;
}
.character-image-container {
display: block;
}
}
/* Animations */
@keyframes spin {
to {
transform: rotate(1turn);
}
}
class RickAndMorty extends React.Component {
state = {
page: 1,
totalPages: 1,
searchTerm: '',
searching: false,
searched: false,
characters: []
}
firstCharacterRef = React.createRef();
handleSearchInput = debounce(searchTerm => this.setState({ page: 1, searchTerm, searching: true }, this.fetchCharacters));
fetchCharacters = () => {
fetch(`https://rickandmortyapi.com/api/character/?page=${this.state.page}&name=${this.state.searchTerm}`)
.then(res => res.json())
.then(data => this.setState({
totalPages: data.info.pages,
characters: data.results,
searching: false,
searched: true
}))
.then(() => this.firstCharacterRef.current.focus())
.catch(() => this.setState({
page: 1,
totalPages: 1,
characters: [],
searching: false,
searched: true
}));
}
changePage = e => {
Array.from(e.target.classList).includes('page-btn-next') ?
this.setState(prevState => ({ page: prevState.page + 1 }), this.fetchCharacters) :
this.setState(prevState => ({ page: prevState.page - 1 }), this.fetchCharacters);
}
render() {
return (
<React.Fragment>
<header>
<h1 className="heading">Rick <span>And</span> Morty</h1>
</header>
<main>
<SearchInput handleSearchInput={ e => this.handleSearchInput(e.target.value.replace(" ", "+")) } />
{ this.state.searching ? <div className="search-loader" /> : null }
{ this.state.searched && !this.state.searching ? <SearchOutput characters={ this.state.characters } firstCharacterRef={ this.firstCharacterRef } /> : null }
{ this.state.totalPages > 1 && !this.state.searching ? <PageNavigation page={ this.state.page } totalPages={ this.state.totalPages } changePage={ this.changePage } /> : null }
</main>
</React.Fragment>
);
}
}
function SearchInput({ handleSearchInput }) {
return (
<div className="search">
<label htmlFor="search-input" className="search-input-label">Character Search:</label>
<input type="text" id="search-input" className="search-input" placeholder="e.g. 'rick'" spellCheck="false" onChange={ handleSearchInput } />
</div>
);
}
function SearchOutput({ characters, firstCharacterRef }) {
return (
<div className="search-output">
{
characters.length > 0 ?
characters.map((character, index) => <Character character={ character } key={ character.id } index={ index } firstCharacterRef={ firstCharacterRef } />) :
<p className="no-results">No Results Found</p>
}
</div>
);
}
function Character({ character, firstCharacterRef, index }) {
return (
<details className="character-details" >
<summary className="character-summary" ref={ index === 0 ? firstCharacterRef : null }>{ character.name }</summary>
<div className="character-container">
<div className="character-info">
<details className="character-info-item" open>
<summary className="character-info-item-summary">Name</summary>
<p className="character-info-item-data">{ character.name }</p>
</details>
<details className="character-info-item" open>
<summary className="character-info-item-summary">Species</summary>
<p className="character-info-item-data">{ character.species }</p>
</details>
<details className="character-info-item" open>
<summary className="character-info-item-summary">Gender</summary>
<p className="character-info-item-data">{ character.gender }</p>
</details>
<details className="character-info-item" open>
<summary className="character-info-item-summary">Location</summary>
<p className="character-info-item-data">{ character.location.name }</p>
</details>
</div>
<div className="character-image-container">
<img className="character-image" src={ character.image } alt={ character.name } />
</div>
</div>
</details>
);
}
function PageNavigation({ page, totalPages, changePage }) {
return (
<div className="page-navigation">
{ page > 1 ? <button className="page-btn page-btn-prev" onClick={ changePage }>Prev Page</button> : null }
{ page < totalPages ? <button className="page-btn page-btn-next" onClick={ changePage }>Next Page</button> : null }
</div>
);
}
function debounce(func, wait = 800) {
let timeout;
return function () {
const context = this,
args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args);
}, wait);
};
}
ReactDOM.render(<RickAndMorty />, document.querySelector('.rick-and-morty'));
console.log('Wubba Lubba Dub-Dub! π΄π»π¦πΎπ¬π«πππΈπΎ');
Also see: Tab Triggers