Pagination
Pages of a long list, the current one in your Model.
Previews are drawn in the theme you made.
Edit itNumbered #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// The page is the application's: `onPage` says where a press goes, and the
// control draws where it is. The window collapses a long run into an ellipsis.
export default example({
title: 'Numbered',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 5 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{ page, pageCount: 12, onPage: page => onMessage(Message.Went({ page })), style },
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Previous and next #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// Previous and next alone, for a feed with no page worth naming.
export default example({
title: 'Previous and next',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 3 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{
page,
pageCount: 12,
numbered: false,
onPage: page => onMessage(Message.Went({ page })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
First and last #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// First and last beside previous and next.
export default example({
title: 'First and last',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 8 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{ page, pageCount: 20, ends: true, onPage: page => onMessage(Message.Went({ page })), style },
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Rows and page size #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ page: Schema.Number, perPage: Schema.Number })
const Message = defineMessageUnion({
Went: { page: Schema.Number },
Sized: { perPage: Schema.Number },
})
const rows = 97
// Which rows the page shows, and a choice of how many a page holds. A new
// size is the application's to apply, with the page that keeps its rows in view.
export default example({
title: 'Rows and page size',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model,
Message,
init: () => ({ model: { page: 2, perPage: 10 } }),
update: (model, message) => ({
model: Message.match(message, {
Went: ({ page }) => ({ ...model, page }),
Sized: ({ perPage }) => ({
perPage,
page: Math.floor(((model.page - 1) * model.perPage) / perPage) + 1,
}),
}),
}),
},
view: ({ style, model, onMessage, h }) =>
Pagination.view(
{
page: model.page,
pageCount: Math.ceil(rows / model.perPage),
onPage: page => onMessage(Message.Went({ page })),
range: {
total: rows,
perPage: model.perPage,
choose: {
sizes: [10, 20, 50],
onPerPage: perPage => onMessage(Message.Sized({ perPage })),
},
},
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Siblings and boundaries #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// Two stops either side of the current page, and two at each end.
export default example({
title: 'Siblings and boundaries',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 20 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{
page,
pageCount: 40,
siblings: 2,
boundaries: 2,
onPage: page => onMessage(Message.Went({ page })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Compact #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// The current page and the count alone, at every width: "5 / 12". The
// default, `compact: 'narrow'`, does this only on a narrow window.
export default example({
title: 'Compact',
slots: PaginationSlots,
recipe: Recipes.Pagination,
pins: { compact: 'always' },
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 5 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{ page, pageCount: 12, onPage: page => onMessage(Message.Went({ page })), style },
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Words of your own #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// Every stop's words, and the name each is given, in your language: text
// with `{blanks}`, merged over `Pagination.words` key by key.
export default example({
title: 'Words of your own',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 3 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{
page,
pageCount: 8,
ends: true,
words: {
label: 'Seitennavigation',
first: 'Erste',
previous: 'Zurück',
next: 'Weiter',
last: 'Letzte',
stop: '{word} Seite',
},
onPage: page => onMessage(Message.Went({ page })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Icons #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// An icon stands in for a stop's word. The word stays the button's accessible name.
export default example({
title: 'Icons',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 4 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{
page,
pageCount: 9,
ends: true,
icons: {
first: outline(h, 'M9 3L4 8l5 5M13 3L8 8l5 5'),
previous: outline(h, 'M10 3L5 8l5 5'),
next: outline(h, 'M6 3l5 5-5 5'),
last: outline(h, 'M7 3l5 5-5 5M3 3l5 5-5 5'),
},
onPage: page => onMessage(Message.Went({ page })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Round and outlined #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// `shape: 'round'` makes each stop a pill, and `treatment: 'outline'` gives
// each the field's outline. Both are CSS over the same markup.
export default example({
title: 'Round and outlined',
slots: PaginationSlots,
recipe: Recipes.Pagination,
pins: { shape: 'round', treatment: 'outline' },
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 4 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{ page, pageCount: 9, onPage: page => onMessage(Message.Went({ page })), style },
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
One page #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// With one page there are no stops to draw: the control draws nothing, unless
// a `range` has a summary to say, as here.
export default example({
title: 'One page',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 1 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ style, model: page, onMessage, h }) =>
Pagination.view(
{
page,
pageCount: 1,
onPage: page => onMessage(Message.Went({ page })),
range: { total: 7, perPage: 10 },
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Configure once #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Pagination, PaginationSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Message = defineMessageUnion({ Went: { page: Schema.Number } })
// The look, windows and words every pager of the application starts from,
// and a named preset over them. The page and `onPage` stay each use's.
const Pills = Recipes.Pagination.style({ shape: 'round' })
const AppPagination = Pagination.configure({
base: { style: Pills, siblings: 2, words: { previous: 'Back' } },
presets: { feed: { numbered: false, words: { next: 'Older' } } },
})
export default example({
title: 'Configure once',
slots: PaginationSlots,
recipe: Recipes.Pagination,
state: {
Model: Schema.Number,
Message,
init: () => ({ model: 5 }),
update: (_, message) => ({ model: message.page }),
},
view: ({ model: page, onMessage, h }) =>
AppPagination.view(
{ page, pageCount: 30, preset: 'feed', onPage: page => onMessage(Message.Went({ page })) },
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Usage
Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.
Related
- Right to left The direction switch above turns the preview over; this is what each widget reads.