Skip to content

Commit

Permalink
Merge pull request #21 from ConnorJamesLow/release/v0.6.1
Browse files Browse the repository at this point in the history
v0.6.1
  • Loading branch information
ConnorJamesLow authored Jun 30, 2022
2 parents 999101c + d86ede0 commit f2d10ff
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 15 deletions.
15 changes: 15 additions & 0 deletions examples/vite-ts-project/src/counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import jsx from 'texsaur';

export const Counter: JSX.Component = () => {
let clicks = 0;
const getMessage = () => `I have been clicked ${clicks} time${clicks++ === 1 ? '' : 's'}.`;

let display = <span>{getMessage()}</span>;
return (
<div class="counter">
<button onclick={() => display.innerHTML = getMessage()}>
{display}
</button>
</div>
);
}
13 changes: 11 additions & 2 deletions examples/vite-ts-project/src/example.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import jsx from 'texsaur';
import { Counter } from './counter';
import svgTest from './svg-test';

interface ExampleProps {
onClick(ev: MouseEvent): any
}

const Example: JSX.Component<ExampleProps> = ({ onClick }, children) => (
<div class="example" onclick={onClick}>{children}</div>
<div class="example" onclick={onClick} aria-label="Example">{children}</div>
);


const div = <div>Hello there.</div> as HTMLElement;
const span = <span>Hello from inside the Example component!</span>;
const content = <>
{div}
<Example onClick={() => alert('clicked!')}>
Hello from inside the Example component!
{span}
</Example>
<Example onClick={() => console.log(svgTest)}>
{svgTest}
</Example>
<Counter />
</> as any as HTMLElement[];


export default <main>{content}</main>;
28 changes: 28 additions & 0 deletions examples/vite-ts-project/src/svg-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import jsx from 'texsaur';

export default <>
<svg viewBox="0 -20 100 50">
<switch>
<text systemLanguage="ar">مرحبا</text>
<text systemLanguage="de,nl">Hallo!</text>
<text systemLanguage="en-us">Howdy!</text>
<text systemLanguage="en-gb">Wotcha!</text>
<text systemLanguage="en-au">G'day!</text>
<text systemLanguage="en">Hello!</text>
<text systemLanguage="es">Hola!</text>
<text systemLanguage="fr">Bonjour!</text>
<text systemLanguage="ja">こんにちは</text>
<text systemLanguage="ru">Привет!</text>
<text></text>
</switch>
</svg>

<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg" stroke="red" fill="grey">
<circle cx="50" cy="50" r="40" />
<circle cx="150" cy="50" r="4" />

<svg viewBox="0 0 10 10" x="200" width="100">
<circle cx="5" cy="5" r="4" />
</svg>
</svg>
</>;
4 changes: 2 additions & 2 deletions package/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "texsaur",
"version": "0.6.0",
"version": "0.6.1",
"description": "",
"types": "./dist/index.d.ts",
"main": "dist/index.js",
Expand Down
5 changes: 2 additions & 3 deletions package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function __jsx(tag: JSX.Tag | JSX.Component, properties: { [key: string]: any }
return svg.parseSvgElement(tag, properties ?? {}, ...children);
}

type Tag = typeof tag;
const element = document.createElement(tag);

let map = (properties ?? {}) as RecursivePartial<JSX.IntrinsicElements[typeof tag]>;
Expand Down Expand Up @@ -65,7 +64,7 @@ function __jsx(tag: JSX.Tag | JSX.Component, properties: { [key: string]: any }
// Event callbacks:
if (/^on/.test(prop)) {
if (typeof value === 'function') {
element.addEventListener(prop.substr(2), map[prop] as any);
element.addEventListener(prop.substring(2), map[prop] as any);
} else {
warn('function', typeof value);
}
Expand All @@ -88,7 +87,7 @@ function __jsx(tag: JSX.Tag | JSX.Component, properties: { [key: string]: any }
}

// append children
for (let child of children) {
for (let child of children.flat()) {
if (child instanceof Node) {
element.appendChild(child);
continue;
Expand Down
18 changes: 11 additions & 7 deletions package/src/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const SVG_TAGS = [
"filter",
"foreignObject",
"g",
"image",
// "image",
"line",
"linearGradient",
"marker",
Expand All @@ -53,11 +53,11 @@ const SVG_TAGS = [
"symbol",
"text",
"textPath",
"title",
// "title",
"tspan",
"use",
"view",
]
];

const SVG_XMLNS = 'http://www.w3.org/2000/svg';
let __experimental_warning_shown = false;
Expand All @@ -67,7 +67,7 @@ export function isSvgTag(tag: string) {
}

export function parseSvgElement(tag: string, attributes: { [key: string]: any }, ...children: Node[]) {
const { document } = JsxDom;
const { document, Node } = JsxDom;
if (!__experimental_warning_shown) {
__experimental_warning_shown = true;
console.warn('texsaur SVG support is experimental!');
Expand All @@ -80,7 +80,7 @@ export function parseSvgElement(tag: string, attributes: { [key: string]: any },
const value = attributes[prop] as any;

// Add attributes:
element.setAttributeNS(null, prop, value);
element.setAttribute(prop, value);
}

// append children
Expand All @@ -89,7 +89,11 @@ export function parseSvgElement(tag: string, attributes: { [key: string]: any },
element.append(...child);
continue;
}
element.appendChild(child);
if (child instanceof Node) {
element.appendChild(child);
} else {
element.append(child);
}
}
return element;
}
}

0 comments on commit f2d10ff

Please sign in to comment.