Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 291082f

Browse files
committed
feat(mdx): add fragments syntax
1 parent f6fc1a9 commit 291082f

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { useEffect, useState, useRef } from 'react';
2+
3+
export const Fragments = ({ children }) => {
4+
const [currentStep, setCurrentStep] = useState(0);
5+
const ref = useRef(currentStep);
6+
7+
const listener = (e) => {
8+
e.stopImmediatePropagation();
9+
if (e.key === 'ArrowRight') {
10+
setCurrentStep(ref.current + 1);
11+
}
12+
};
13+
14+
useEffect(() => {
15+
ref.current = currentStep;
16+
}, [currentStep]);
17+
18+
useEffect(() => {
19+
document.addEventListener('keydown', listener, {
20+
capture: false,
21+
passive: false,
22+
});
23+
return () => {
24+
document.removeEventListener('keydown', listener);
25+
};
26+
}, []);
27+
28+
return React.Children.map(children, (child, i) =>
29+
React.cloneElement(child, { style: { visibility: i >= currentStep ? 'hidden' : 'initial' } })
30+
);
31+
};

packages/client/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './components/SlideCore';
2+
export * from './components/Fragments';

packages/mdx-loader/src/mdxPlugin.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function mdxPlugin() {
2929
slides.push({ slide, props, background });
3030
slide = [];
3131
props = {};
32-
background = 0; // why 0? null, undefined, '' are omitted at client side
32+
background = 0; // why 0? because null, undefined, '' are omitted at client side
3333
return;
3434
}
3535

@@ -68,6 +68,22 @@ function mdxPlugin() {
6868
props.contents = true;
6969
return;
7070
}
71+
if (prefix === 'fragments-start') {
72+
slide.push({
73+
...n,
74+
type: 'jsx',
75+
value: '<Client.Fragments>',
76+
});
77+
return;
78+
}
79+
if (prefix === 'fragments-end') {
80+
slide.push({
81+
...n,
82+
type: 'jsx',
83+
value: '</ Client.Fragments>',
84+
});
85+
return;
86+
}
7187
if (prefix === 'note') {
7288
props.note = valueStr.replace(/[&<>"']/gim, (m) => escapeMap[m]).replace(/\n/g, '\\n');
7389
return;
@@ -196,6 +212,9 @@ function mdxPlugin() {
196212
value: `
197213
import React from 'react';
198214
import { mdx } from '@mdx-js/react';
215+
// don't import as named to avoid using makeShortcode by mdx
216+
import * as Client from '@fusuma/client';
217+
199218
export const slides = [${res.jsx.join(',\n')}];
200219
export const backgrounds = [${res.background.join(',\n')}];
201220
export const fusumaProps = [${res.fusumaProps.join(',\n')}];`,

samples/debug/slides/06-blocks.md

+14
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ middle
1919
outer
2020

2121
<!-- block-end -->
22+
23+
---
24+
25+
<!-- fragments-start -->
26+
27+
1
28+
29+
2
30+
31+
3
32+
33+
4
34+
35+
<!-- fragments-end -->

0 commit comments

Comments
 (0)