Components
Direction
Sets the reading direction for what is inside it, so logical spacing and the components follow right-to-left text. Code stays left to right.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add direction<script type="module" src="/spyglass-ds/elements/direction.js"></script>Usage
An interface in Arabic or Hebrew, or one block of right-to-left text inside a left-to-right page.
Never
- Mirroring the logo or the brand light
- Flipping code, IDs or numbers
Props
The web component takes the same as attributes or properties (<spyglass-direction>); title is heading there.
<Direction>
| Prop | Type | Default |
|---|---|---|
dir | Dir ltr or rtl. | 'ltr' |
children | ReactNode |
Examples
The same example in React and as web components; both render identically.
import { Badge } from '@/react/components/ui/Badge'
import { Breadcrumb } from '@/react/components/ui/Breadcrumb'
import { Direction } from '@/react/components/ui/Direction'
// The same card read left to right and right to left: logical spacing and the breadcrumb follow; code stays left to
// right.
function Sample() {
return (
<div className="flex flex-col gap-3 rounded-container bg-surface-raised p-6">
<Breadcrumb items={[{ label: 'Boards', href: '#boards' }, { label: 'Acme skincare' }]} />
<div className="flex items-center gap-2">
<Badge variant="outline" dot="green">Running</Badge>
<span className="type-ui text-fg-muted">since March</span>
</div>
<code className="self-start rounded-ds-sm bg-surface px-2 py-1 type-code text-fg-muted">board_8f2k1</code>
</div>
)
}
export default function Rtl() {
return (
<div className="grid w-200 max-w-full grid-cols-2 gap-4 max-sm:grid-cols-1">
<Direction dir="ltr"><Sample /></Direction>
<Direction dir="rtl"><Sample /></Direction>
</div>
)
}<!-- The same card read left to right and right to left: logical spacing and the breadcrumb follow; code stays left to
right. -->
<div class="grid w-200 max-w-full grid-cols-2 gap-4 max-sm:grid-cols-1">
<spyglass-direction dir="ltr">
<div class="flex flex-col gap-3 rounded-container bg-surface-raised p-6">
<spyglass-breadcrumb items='[{"label":"Boards","href":"#boards"},{"label":"Acme skincare"}]'></spyglass-breadcrumb>
<div class="flex items-center gap-2">
<spyglass-badge variant="outline" dot="green">Running</spyglass-badge>
<span class="type-ui text-fg-muted">since March</span>
</div>
<code class="self-start rounded-ds-sm bg-surface px-2 py-1 type-code text-fg-muted">board_8f2k1</code>
</div>
</spyglass-direction>
<spyglass-direction dir="rtl">
<div class="flex flex-col gap-3 rounded-container bg-surface-raised p-6">
<spyglass-breadcrumb items='[{"label":"Boards","href":"#boards"},{"label":"Acme skincare"}]'></spyglass-breadcrumb>
<div class="flex items-center gap-2">
<spyglass-badge variant="outline" dot="green">Running</spyglass-badge>
<span class="type-ui text-fg-muted">since March</span>
</div>
<code class="self-start rounded-ds-sm bg-surface px-2 py-1 type-code text-fg-muted">board_8f2k1</code>
</div>
</spyglass-direction>
</div>Source
'use client'
import type { ReactNode } from 'react'
import { DirectionContext, type Dir } from '@/react/hooks/useDirection'
// shadcn's direction provider: sets the reading direction for what is inside it (the `dir` attribute, so logical
// utilities such as ps-, me- and text-start follow it) and provides it to components through useDirection(). Code
// stays left to right: pre, code and kbd inside it keep `ltr`.
export interface DirectionProps {
/** ltr or rtl. */
dir?: Dir
children?: ReactNode
}
export function Direction({ dir = 'ltr', children }: DirectionProps) {
return (
<DirectionContext.Provider value={dir}>
<style href="ds-direction" precedence="ds">{css}</style>
<div data-ds="direction" className="ds-dir" dir={dir}>{children}</div>
</DirectionContext.Provider>
)
}
export default Direction
const css = `
.ds-dir :is(pre, code, kbd) { direction: ltr; unicode-bidi: isolate; }
`'use client'
import { createContext, useContext } from 'react'
// The reading direction a Direction wrapper provides to what is inside it, so a component can flip what it draws
// (an arrow, a slide) for right-to-left text. Outside any wrapper it reads `ltr`.
export type Dir = 'ltr' | 'rtl'
export const DirectionContext = createContext<Dir>('ltr')
export function useDirection() {
return useContext(DirectionContext)
}import { html, type PropertyValues } from 'lit'
import { DsElement, define, tagName } from '@/elements/lib/element'
import styles from 'virtual:tw'
// Direction.tsx as <spyglass-direction>: sets the reading direction for what is inside it (the `dir` attribute, so
// logical utilities such as ps-, me- and text-start follow it), and elements inside read it through the direction
// controller (React's useDirection). Code stays left to right: pre, code and kbd inside it keep `ltr`. Set `dir` as on
// any element (the attribute or the property).
// The content is the page's (slotted), out of reach of the shadow root's styles: the rule for code goes to the
// document (or the shadow root the wrapper sits in) once.
const sheets = new WeakSet<Document | ShadowRoot>()
function adoptCodeRule(root: Node) {
if (!(root instanceof Document || root instanceof ShadowRoot) || sheets.has(root)) return
const sheet = new CSSStyleSheet()
sheet.replaceSync(`${tagName('direction')} :is(pre, code, kbd) { direction: ltr; unicode-bidi: isolate; }`)
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet]
sheets.add(root)
}
/**
* @summary Sets the reading direction for what is inside it.
* @slot - The content.
* @fires direction-change - The direction changed (detail.value).
*/
export class Direction extends DsElement {
static styles = [DsElement.styles, styles]
// `dir` is HTML's own attribute; the element reads it into `direction` and draws it on its box.
static properties = { direction: { attribute: 'dir' } }
/** ltr or rtl (the `dir` attribute). */
direction: 'ltr' | 'rtl' = 'ltr'
connectedCallback() {
super.connectedCallback()
adoptCodeRule(this.getRootNode())
}
protected updated(changed: PropertyValues<this>) {
super.updated(changed)
if (changed.has('direction') && changed.get('direction') !== undefined) this.emit('direction-change', { value: this.direction })
}
render() {
return html`<div data-ds="direction" class="ds-dir" dir=${this.direction === 'rtl' ? 'rtl' : 'ltr'}><slot></slot></div>`
}
}
define('direction', Direction)Reference: shadcn's direction provider; nothing visual of its own..