Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
	<h1>Pagination Sequence Integration Demo (React)</h1>
	<p>A Pagination Component that processes and renders the output of <a href="https://github.com/bramus/js-pagination-sequence" target="_top"><code>@bramus/pagination-sequence</code></a></p>
	<div id="root">
		<p style="text-align: center;"><em>[Loading Demo…]</em></p> 
	</div>
	<p><em>Demo for <a href="https://github.com/bramus/js-pagination-sequence" target="_top">https://github.com/bramus/js-pagination-sequence</a></em></p>
</main>
              
            
!

CSS

              
                
.pagination {
	list-style: none;
	margin: 0 auto;
	padding: 0;
	
	display: flex;
	gap: 0.5em;
	flex-wrap: nowrap;
	width: fit-content;
}

.pagination li {
	text-decoration: none;
	padding: 0;
	text-align: center;
}

.pagination li > * {
	--size: 2em;
	display: inline-block;
	width: var(--size);
	line-height: var(--size);
	aspect-ratio: 1;
	border: 1px solid currentcolor;
}

.pagination li a {
	color: blue;
	text-decoration: none;
}
.pagination li a:hover,
.pagination li a:focus {
	background: #0000FF22;
}

.pagination li[data-pagination-current] a {
	color: #fff;
	background: blue;
	border-color: blue;
}

.pagination li[data-pagination-current] a:hover,
.pagination li[data-pagination-current] a:focus {
	color: blue;
	background: #0000FF22;
}

.pagination li[data-pagination-ellipsis],
.pagination li[data-pagination-disabled] {
	color: #ccc;
}

.pagination li[data-pagination-ellipsis] > *,
.pagination li[data-pagination-disabled] > *,
.pagination li[data-pagination-first] > *,
.pagination li[data-pagination-prev] > *,
.pagination li[data-pagination-next] > *,
.pagination li[data-pagination-last] > * {
	border-color: transparent;
}

/* Instructions + Tweak Settings */
h2 {
	margin: 3rem auto 0.75rem;
}
fieldset {
	display: grid;
	grid-template-columns: 1fr 2fr;
	gap: 0.5em;
}
label {
	text-align: right;
}


/* General Styles */
html, body {
	height: 100%;
	width: 100%;
    margin: 0;
    padding: 0;
}
body {
	display: grid;
	place-items: center;
}
#root {
	margin: 4em 0;
}
main > :not(root) {
	text-align: center;
}
main > :not(#root) a {
	color: blue;
}
              
            
!

JS

              
                import React from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";
import { generate } from "https://cdn.skypack.dev/@bramus/pagination-sequence";

console.clear();

const BASE_URL = "#";

const PaginationEntry = ({value, onEntryClick = null, label = null, title = null, isCurrent = false, isDisabled = false, ...props}) => {
    label ??= value;
    title ??= `Go to page ${value}`;

    const onClick = (e) => {
        e.stopPropagation();
        e.preventDefault();

        e.target.blur();

        if (onEntryClick) {
            onEntryClick(value);
        }
    };

    if (value == "…") {
        return (
            <li data-pagination-ellipsis {...props}><span>{label}</span></li>
        );
    }

    if (isDisabled) {
        return (
            <li data-pagination-disabled {...props}><span>{label}</span></li>
        );
    }

    if (isCurrent) {
        props["data-pagination-current"] = true;
    }

    return (
        <li {...props}>
            <a href={`${BASE_URL}/page/${value}`} title={title} onClick={onClick}>{label}</a>
        </li>
    );
};

const Pagination = ({curPage, numPages, sequence, onEntryClick = null, showFirstLastArrows = true, showNextPrevArrows = true }) => {
    return (
        <ul className="pagination">
            {showFirstLastArrows && <PaginationEntry data-pagination-first onEntryClick={onEntryClick} value={1} title="Go to First Page" label="&laquo;" isDisabled={curPage === 1} /> }
            {showNextPrevArrows && <PaginationEntry data-pagination-prev onEntryClick={onEntryClick} value={curPage - 1} title="Go to Previous Page" label="&lsaquo;" isDisabled={curPage === 1} />}
            {sequence.map((val, idx) => (
                <PaginationEntry key={`page-${val == "…" ? `…-${idx}` : val}`} onEntryClick={onEntryClick} value={val} isCurrent={val == curPage} />
            ))}
            {showNextPrevArrows && <PaginationEntry data-pagination-next onEntryClick={onEntryClick} value={curPage + 1} title="Go to Next Page" label="&rsaquo;" isDisabled={curPage === numPages} />}
            {showFirstLastArrows && <PaginationEntry data-pagination-next onEntryClick={onEntryClick} value={numPages} title="Go to Last Page" label="&raquo;" isDisabled={curPage === numPages} />}
        </ul>
    );
};

const Demo = () => {
    const [curPage, setCurPage] = React.useState(1);
    const [numPages, setNumPages] = React.useState(50);
    const [numPagesAtEdges, setNumPagesAtEdges] = React.useState(2);
    const [numPagesAroundCurrent, setNumPagesAroundCurrent] = React.useState(2);
    
    const [showNextPrevArrows, setShowNextPrevArrows] = React.useState(true);
    const [showFirstLastArrows, setShowFirstLastArrows] = React.useState(false);

    const [lastAction, setLastAction] = React.useState("💡 Use the form controls below to tweak the pagination sequence params");
    const [sequence, setSequence] = React.useState([]);

    // When dragging numPages, make sure curPage is capped to it when dragging down
    React.useEffect(() => {
        if (curPage > numPages) {
            setCurPage(numPages);
        }
    }, [numPages]);

    // When dragging curPage up, make sure the range of numPages is extended too
    React.useEffect(() => {
        if (curPage > numPages) {
            setNumPages(curPage);
        }
    }, [curPage]);

    // Click handler
    const handleEntryClick = (value) => {
        setCurPage(value);
        setLastAction(`⚡️ Clicked: Go to Page ${value}`);
    };

    const setValue = (setFunction) => (e) => setFunction(parseInt(e.target.value));
    const toggleChecked = (setFunction, curValue) => (e) => setFunction(!curValue);
    
    // Generate a sequence when params change
    React.useEffect(() => {
        setSequence(generate(curPage, numPages, numPagesAtEdges, numPagesAroundCurrent));
    }, [curPage, numPages, numPagesAtEdges, numPagesAroundCurrent]);

    return (
        <div>
            <h2>Input Params</h2>
            <form>
                <fieldset>
                    <legend>Tweak <code>@bramus/pagination-sequence</code> params</legend>
                    <label htmlFor="curPage"><code>curPage = <output>{curPage}</output></code></label>
                    <input type="range" id="curPage" onInput={setValue(setCurPage)} value={curPage} min={1} max={50} step="1" />
                    
                    <label htmlFor="numPages"><code>numPages = <output>{numPages}</output></code></label>
                    <input type="range" id="numPages" onInput={setValue(setNumPages)} value={numPages} min={1} max={50} step="1" />

                    <label htmlFor="numPagesAtEdges"><code>numPagesAtEdges = <output>{numPagesAtEdges}</output></code></label>
                    <input type="range" id="numPagesAtEdges" onInput={setValue(setNumPagesAtEdges)} defaultValue={numPagesAtEdges} min={0} max={3} step="1" />

                    <label htmlFor="numPagesAroundCurrent"><code>numPagesAroundCurrent = <output>{numPagesAroundCurrent}</output></code></label>
                    <input type="range" id="numPagesAroundCurrent" onInput={setValue(setNumPagesAroundCurrent)} defaultValue={numPagesAroundCurrent} min={0} max={3} step="1" />
                </fieldset>
            </form>

            <h2>Raw <code>@bramus/pagination-sequence</code> Output</h2>
            <p><code><output>[{sequence.join(',')}]</output></code></p>

            <h2>Rendered with Component</h2>
            <Pagination {...{
                curPage,
                numPages,
                sequence,
                onEntryClick: handleEntryClick,
                showFirstLastArrows,
                showNextPrevArrows,
            }} />

            <h2>Component Configuration</h2>
            <form>
                <fieldset>
                    <legend>Tweak Component Appearance</legend>

                    <label htmlFor="showNextPrevArrows"><code>showNextPrevArrows</code></label>
                    <input type="checkbox" id="showNextPrevArrows" onInput={toggleChecked(setShowNextPrevArrows, showNextPrevArrows)} checked={showNextPrevArrows} />

                    <label htmlFor="showFirstLastArrows"><code>showFirstLastArrows</code></label>
                    <input type="checkbox" id="showFirstLastArrows" onInput={toggleChecked(setShowFirstLastArrows, showFirstLastArrows)} checked={showFirstLastArrows} />
                </fieldset>
            </form>
        </div>
    );
};

ReactDOM.render(<Demo />, document.getElementById("root"));

              
            
!
999px

Console