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

feat: support customize styles and scripts #2229

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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: 15 additions & 1 deletion packages/rax-document/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,22 @@ function Data(props, context) {
// Named by role rather than implementationm, so component name are `Style` rather than `Styles`.
function Style(props, context) {
const { __styles = [] } = context;
const consumer = getConsumer(props);

if (typeof consumer === 'function') {
return consumer(__styles);
}

return __styles.map((src, index) => <link {...props} rel="stylesheet" href={src} key={`style_${index}`} />);
}

function Script(props, context) {
const { __scripts = [] } = context;
const consumer = getConsumer(props);

if (typeof consumer === 'function') {
return consumer(__scripts);
}

// props such as type can be passed to script tag
// script default crossorigin value is anonymous
Expand All @@ -72,7 +82,7 @@ function App(props, context) {
return route.path == pagePath;
});

const consumer = Array.isArray(props.children) ? props.children[0] : props.children;
const consumer = getConsumer(props);

if (typeof consumer === 'function') {
return consumer(currentPageInfo);
Expand All @@ -81,6 +91,10 @@ function App(props, context) {
return props.children;
}

function getConsumer(props) {
return Array.isArray(props.children) ? props.children[0] : props.children
}

export {
Root,
Data,
Expand Down