Skip to content

Commit

Permalink
🪈 propagate class value through to react on container type directiv…
Browse files Browse the repository at this point in the history
…es (#414)

* 🎻 add `node.class` consistently to container & image type directives

* 📗 changeset
  • Loading branch information
stevejpurves authored Jul 2, 2024
1 parent f685454 commit 3775181
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-ads-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-react': patch
---

Allow class option to flow through to containers for `aside`, `code`, `image`, `video`, `iframe`.
11 changes: 6 additions & 5 deletions packages/myst-to-react/src/aside.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NodeRenderer } from '@myst-theme/providers';
import { MyST } from './MyST.js';
import type { GenericNode } from 'myst-common';
import classNames from 'classnames';

type Aside = {
type: 'aside';
Expand Down Expand Up @@ -28,20 +29,20 @@ function getAsideClass(kind?: string) {

export const AsideRenderer: NodeRenderer<Aside> = ({ node }) => {
const [title, ...rest] = node.children as GenericNode[];
const className = getAsideClass(node.kind);
const classes = getAsideClass(node.kind);
if (title.type !== 'admonitionTitle') {
return (
<aside className={className.container}>
<aside className={classNames(classes.container, node.class)}>
<MyST ast={node.children} />
</aside>
);
}
return (
<aside className={className.container}>
<div className={className.title}>
<aside className={classNames(classes.container, node.class)}>
<div className={classes.title}>
<MyST ast={title} />
</div>
<div className={className.body}>
<div className={classes.body}>
<MyST ast={rest} />
</div>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-react/src/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const code: NodeRenderer<Code & { executable: boolean }> = ({ node }) => {
shadow
border={node.executable}
background={!node.executable}
className={classNames({ hidden: node.visibility === 'remove' })}
className={classNames({ hidden: node.visibility === 'remove' }, node.class)}
/>
);
};
Expand Down
3 changes: 2 additions & 1 deletion packages/myst-to-react/src/iframe.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NodeRenderer } from '@myst-theme/providers';
import classNames from 'classnames';

/** This is duplicated in image, but a bit different logic */
function getStyleValue(width?: number | string): number | undefined {
Expand Down Expand Up @@ -27,7 +28,7 @@ export const IFrame: NodeRenderer = ({ node }) => {
<div
id={node.label || undefined}
style={{ textAlign: node.align || 'center' }}
className="leading-[0]"
className={classNames('leading-[0]', node.class)}
>
<div
className="relative inline-block"
Expand Down
18 changes: 16 additions & 2 deletions packages/myst-to-react/src/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ function alignToMargin(align: string) {
}

function Video({
className,

Check warning on line 41 in packages/myst-to-react/src/image.tsx

View workflow job for this annotation

GitHub Actions / lint

'className' is defined but never used
id,
src,
urlSource,
align = 'center',
width,
height,
}: {
className?: string;
id?: string;
src: string;
urlSource?: string;
Expand All @@ -54,6 +56,7 @@ function Video({
}) {
return (
<video
// className={className}
id={id}
style={{
width: getStyleValue(width),
Expand All @@ -73,6 +76,7 @@ function Video({
}

function Picture({
className,
id,
src,
srcOptimized,
Expand All @@ -82,6 +86,7 @@ function Picture({
width,
height,
}: {
className?: string;
id?: string;
src: string;
srcOptimized?: string;
Expand All @@ -93,7 +98,15 @@ function Picture({
}) {
if (src.endsWith('.mp4') || urlSource?.endsWith('.mp4')) {
return (
<Video id={id} width={width} height={height} align={align} src={src} urlSource={urlSource} />
<Video
className={className}
id={id}
width={width}
height={height}
align={align}
src={src}
urlSource={urlSource}
/>
);
}
const image = (
Expand All @@ -111,7 +124,7 @@ function Picture({
);
if (!srcOptimized) return image;
return (
<picture>
<picture className={className}>
<source srcSet={srcOptimized} type="image/webp" />
{image}
</picture>
Expand All @@ -121,6 +134,7 @@ function Picture({
export const Image: NodeRenderer<ImageNode> = ({ node }) => {
return (
<Picture
className={node.class}
id={node.html_id || node.identifier || node.key}
src={node.url}
srcOptimized={(node as any).urlOptimized}
Expand Down

0 comments on commit 3775181

Please sign in to comment.