Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪜 Use BlockDepth provider to scope grid layout to top-level blocks #547

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/jupyter/src/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
NotebookRunCell,
NotebookRunCellSpinnerOnly,
} from './controls/index.js';
import { useGridSystemProvider, usePageKind } from '@myst-theme/providers';
import {
useBlockDepth,
BlockDepthProvider,
useGridSystemProvider,
usePageKind,
} from '@myst-theme/providers';
import type { NodeRenderers, NodeRenderer } from '@myst-theme/providers';

export function NotebookBlock({
Expand All @@ -19,13 +24,16 @@ export function NotebookBlock({
node: GenericParent;
className?: string;
}) {
const depth = useBlockDepth();
const pageKind = usePageKind();
const grid = useGridSystemProvider();
const subGrid = node.visibility === 'hide' ? '' : `${grid} subgrid-gap col-screen`;
const dataClassName = typeof node.data?.class === 'string' ? node.data?.class : undefined;
// Hide the subgrid if either the dataClass or the className exists and includes `col-`
const noSubGrid =
(dataClassName && dataClassName.includes('col-')) || (className && className.includes('col-'));
depth > 0 ||
(dataClassName && dataClassName.includes('col-')) ||
(className && className.includes('col-'));
const block = (
<div
key={`block-${id}`}
Expand All @@ -50,7 +58,9 @@ export function NotebookBlock({
</div>
</>
)}
<MyST ast={node.children} />
<BlockDepthProvider depth={depth + 1}>
<MyST ast={node.children} />
</BlockDepthProvider>
</div>
);
if (node.visibility === 'hide') {
Expand Down
11 changes: 8 additions & 3 deletions packages/myst-to-react/src/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Details } from './dropdown.js';
import { MyST } from './MyST.js';
import type { GenericParent } from 'myst-common';
import classNames from 'classnames';
import { useGridSystemProvider } from '@myst-theme/providers';
import { useBlockDepth, useGridSystemProvider, BlockDepthProvider } from '@myst-theme/providers';
import type { NodeRenderer } from '@myst-theme/providers';

export function Block({
Expand All @@ -14,12 +14,15 @@ export function Block({
node: GenericParent;
className?: string;
}) {
const depth = useBlockDepth();
const grid = useGridSystemProvider();
const subGrid = node.visibility === 'hide' ? '' : `${grid} subgrid-gap col-screen`;
const dataClassName = typeof node.data?.class === 'string' ? node.data?.class : undefined;
// Hide the subgrid if either the dataClass or the className exists and includes `col-`
const noSubGrid =
(dataClassName && dataClassName.includes('col-')) || (className && className.includes('col-'));
depth > 0 ||
(dataClassName && dataClassName.includes('col-')) ||
(className && className.includes('col-'));
const block = (
<div
key={`block-${id}`}
Expand All @@ -29,7 +32,9 @@ export function Block({
hidden: node.visibility === 'remove',
})}
>
<MyST ast={node.children} />
<BlockDepthProvider depth={depth + 1}>
<MyST ast={node.children} />
</BlockDepthProvider>
</div>
);
if (node.visibility === 'hide') {
Expand Down
22 changes: 22 additions & 0 deletions packages/providers/src/block.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { createContext, useContext } from 'react';

type BlockDepth = {
depth: number;
};
const BlockDepthContext = createContext<BlockDepth | undefined>(undefined);

export function BlockDepthProvider({
children,
depth,
}: {
children: React.ReactNode;
depth: number;
}) {
return <BlockDepthContext.Provider value={{ depth }}>{children}</BlockDepthContext.Provider>;
}

export function useBlockDepth(): number {
const context = useContext(BlockDepthContext);
const { depth } = context ?? { depth: 0 };
return depth;
}
1 change: 1 addition & 0 deletions packages/providers/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './tabs.js';
export * from './xref.js';
export * from './renderers.js';
export * from './project.js';
export * from './block.js';
Loading