Skip to content

Commit

Permalink
Added basic Next.js boilerplate + simple openjscad-react example
Browse files Browse the repository at this point in the history
  • Loading branch information
aeksco committed Oct 20, 2020
1 parent 3b930f9 commit 0cb68be
Show file tree
Hide file tree
Showing 14 changed files with 6,212 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.next/
out/
yarn-error.log
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# openjscad-react-next-starter

A web starter project with OpenJSCAD, React, Next.js, TypeScript, and Netlify. Built with [openjscad-react](https://github.com/aeksco/openjscad-react).
4 changes: 4 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Doc: https://www.netlify.com/docs/netlify-toml-reference/
[build]
command = "yarn build && yarn export"
publish = "out"
2 changes: 2 additions & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "openjscad-react-next-starter",
"version": "0.1.0",
"description": "A web starter project with OpenJSCAD React, Next.js, TypeScript, TailwindCSS and Netlify",
"main": "index.js",
"scripts": {
"dev": "next",
"build": "next build",
"export": "next export",
"start": "next start"
},
"author": "Alexander Schwartzberg",
"license": "MIT",
"dependencies": {
"@types/downloadjs": "^1.4.2",
"downloadjs": "^1.4.7",
"lodash.debounce": "^4.0.8",
"next": "^9.0.1",
"openjscad-react": "^0.0.1-canary-04",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"tailwindcss": "^1.8.10"
},
"devDependencies": {
"@types/lodash.debounce": "^4.0.6",
"@types/node": "^12.6.2",
"@types/react": "^16.8.23",
"@types/react-dom": "^16.8.4",
"postcss-preset-env": "^6.7.0",
"typescript": "^3.5.3"
}
}
18 changes: 18 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";
import { PageHeader } from "../src/PageHeader";
import App from "next/app";
import "../styles/index.css";

export default class extends App {
render() {
const { Component, pageProps } = this.props;
return (
<React.Fragment>
<PageHeader />
<div className="container mx-auto">
<Component {...pageProps} />
</div>
</React.Fragment>
);
}
}
19 changes: 19 additions & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Document, { Html, Head, Main, NextScript } from "next/document";

class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<title>OpenJSCAD React Next.js Starter</title>
</Head>
<body className="bg-gray-200">
<Main />
<NextScript />
</body>
</Html>
);
}
}

export default MyDocument;
86 changes: 86 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React from "react";
import dynamic from "next/dynamic";
import { OpenJSCADProps } from "openjscad-react/dist/types";

const STLB_FORMAT = {
name: "stlb",
displayName: "STL (Binary)",
description: "STereoLithography, Binary",
extension: "stl",
mimetype: "application/sla",
convertCSG: true,
convertCAG: false
};

// Dynamic import
const OpenJSCAD: React.ComponentType<OpenJSCADProps> = dynamic(
() => import("openjscad-react/dist").then(mod => mod.OpenJSCAD),
{ ssr: false }
);

// // // //

export const DEFAULT_SCRIPT = `
function main () {
return union(
difference(
cube({size: 3, center: true}),
sphere({r: 2, center: true})
),
intersection(
sphere({r: 1.3, center: true}),
cube({size: 2.1, center: true})
)
).translate([0, 0, 1.5]).scale(10);
}
`;

const Index = () => {
if (typeof window == "undefined" || typeof document == "undefined") {
return null;
}

return (
<OpenJSCAD
className="grid grid-cols-1 mt-5"
jscadScript={DEFAULT_SCRIPT}
viewerOptions={{}}
// defaultOutputFileFormat="stl"
>
{({ viewerElement, outputFile }) => {
// NOTE - processor is null, must be fixed
// if (processor !== null) {
// // setTimeout(() => {
// // processor.generateOutputFile(STLB_FORMAT);
// // }, 3000);
// }

return (
<div>
{viewerElement}
{/* <button
className="btn btn-blue"
onClick={() => {
processor.generateOutputFile(STLB_FORMAT);
}}
>
Download STL
</button> */}
{outputFile !== null && (
<a
href={outputFile.data}
download="stl-export.stl"
className="btn btn-blue"
>
Download STL
</a>
)}
<pre>{JSON.stringify({ outputFile }, null, 4)}</pre>
</div>
);
}}
</OpenJSCAD>
);
};

export default Index;
3 changes: 3 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: ["tailwindcss", "postcss-preset-env"]
};
37 changes: 37 additions & 0 deletions src/PageHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from "react";

// // // //

export function PageHeader() {
return (
<nav className="flex items-center justify-between flex-wrap bg-blue-400 p-6">
<div className="flex items-center flex-no-shrink text-white mr-6">
<span className="font-semibold text-xl tracking-tight">
OpenJSCAD React Next.js Starter
</span>
</div>
<div className="w-full block flex-grow lg:flex lg:items-center lg:w-auto">
<div className="text-sm lg:flex-grow">
<a
href="#responsive-header"
className="block mt-4 lg:inline-block lg:mt-0 text-blue-300 hover:text-white mr-4"
>
Documentation
</a>
<a
href="#responsive-header"
className="block mt-4 lg:inline-block lg:mt-0 text-blue-300 hover:text-white mr-4"
>
Examples
</a>
<a
href="#responsive-header"
className="block mt-4 lg:inline-block lg:mt-0 text-blue-300 hover:text-white"
>
Blog
</a>
</div>
</div>
</nav>
);
}
14 changes: 14 additions & 0 deletions styles/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

/* Tailwind abstractions */
.btn {
@apply font-bold py-2 px-4 rounded;
}
.btn-blue {
@apply bg-blue-500 text-white;
}
.btn-blue:hover {
@apply bg-blue-700;
}
9 changes: 9 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
future: {},
purge: [],
theme: {
extend: {}
},
variants: {},
plugins: []
};
32 changes: 32 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compilerOptions": {
"allowJs": true,
"alwaysStrict": true,
"esModuleInterop": true,
"isolatedModules": true,
"jsx": "preserve",
"lib": [
"dom",
"es2017"
],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strict": true,
"target": "esnext",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
]
}
Loading

0 comments on commit 0cb68be

Please sign in to comment.