Skip to content

Commit

Permalink
feat: head tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
htmujahid committed May 13, 2024
1 parent b51111d commit 764f669
Show file tree
Hide file tree
Showing 18 changed files with 243 additions and 17 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"private": true,
"scripts": {
"dev:sonnet-function": "turbo run dev --filter=sonnet-function",
"build": "turbo run build",
"dev": "turbo run dev",
"lint": "turbo run lint",
Expand All @@ -20,7 +19,7 @@
},
"packageManager": "npm@10.2.4",
"workspaces": [
"playgrounds/*",
"playground/*",
"packages/*",
"tooling/*"
]
Expand Down
2 changes: 1 addition & 1 deletion packages/sonnet-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sonnetjs/core",
"version": "0.0.26",
"version": "0.0.27",
"files": [
"dist"
],
Expand Down
16 changes: 12 additions & 4 deletions packages/sonnet-core/src/abstract/SonnetComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SonnetGet } from '@sonnetjs/shared';
import { SonnetGet, SonnetHead } from '@sonnetjs/shared';

export interface SonnetComponentProps {
_id: string;
Expand Down Expand Up @@ -63,11 +63,19 @@ export default abstract class SonnetComponent {
return this;
}

public static script() {}
static head(): SonnetHead {
return '';
}

head(): SonnetHead {
return '';
}

static script() {}

public script() {}
script() {}

public get(): SonnetGet {
get(): SonnetGet {
return '';
}
}
19 changes: 14 additions & 5 deletions packages/sonnet-core/src/core/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ export class EventEmitter {
return EventEmitter.instance;
}

on(eventName: string, listener: (...args: unknown[]) => void) {
on<T>(
eventName: string,
listener: (...args: unknown[]) => T extends void ? void : T,
) {
if (!this.events[eventName]) {
this.events[eventName] = [];
}
this.events[eventName].push(listener);
}

once(eventName: string, listener: (...args: unknown[]) => void) {
once<T>(
eventName: string,
listener: (...args: unknown[]) => T extends void ? void : T,
) {
if (!this.events[eventName]) {
this.events[eventName] = [];
}
Expand All @@ -29,7 +35,10 @@ export class EventEmitter {
}
}

off(eventName: string, listener?: (...args: unknown[]) => void) {
off<T>(
eventName: string,
listener?: (...args: unknown[]) => T extends void ? void : T,
) {
const listeners = this.events[eventName];
if (listeners && !listener) {
delete this.events[eventName];
Expand All @@ -38,10 +47,10 @@ export class EventEmitter {
}
}

emit(eventName: string, ...args: unknown[]) {
emit<T>(eventName: string, ...args: unknown[]): T[] | void {
const listeners = this.events[eventName];
if (listeners) {
listeners.forEach((listener) => listener(...args));
return listeners.map((listener) => listener(...args) as T);
}
}

Expand Down
12 changes: 11 additions & 1 deletion packages/sonnet-core/src/core/SonnetApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { isBrowser } from '@sonnetjs/shared';
import { SonnetHead, isBrowser } from '@sonnetjs/shared';

import { EventEmitter } from './Event';
import SonnetComponent from '../abstract/SonnetComponent';
Expand Down Expand Up @@ -94,6 +94,16 @@ export class SonnetApp {
}
}

const heads = event.emit<SonnetHead>('head');

heads?.forEach((head) => {
if (typeof head === 'string') {
document.head.innerHTML += head;
} else if (head instanceof Element) {
document.head.appendChild(head);
}
});

event.emit('script');
event.off('script');
}
Expand Down
15 changes: 13 additions & 2 deletions packages/sonnet-core/src/core/factory.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { SonnetHead } from '@sonnetjs/shared';

import { EventEmitter } from './Event';
import SonnetComponent from '../abstract/SonnetComponent';

const event = EventEmitter.getInstance();

interface Component<T> {
new (args?: T): SonnetComponent;
head?: () => void;
script?: () => void;
staticScript?: () => void;
}

export function $component<T>(component: Component<T>) {
return (args?: T) => {
const instance = new component(args);
// head tags
if (component.head && component.head.toString() !== 'head(){return""}') {
event.once<SonnetHead>('head', component.head as () => SonnetHead);
}
if (instance.head && instance.head.toString() !== 'head(){return""}') {
event.on<SonnetHead>('head', instance.head.bind(instance));
}
// scripts
if (component.script && component.script.toString() !== 'script(){}') {
event.once('script', component.script);
}
if (instance.script) {
if (instance.script && instance.script.toString() !== 'script(){}') {
event.on('script', instance.script.bind(instance));
}

return instance as SonnetComponent;
};
}
2 changes: 1 addition & 1 deletion packages/sonnet-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sonnetjs/shared",
"version": "0.0.2",
"version": "0.0.3",
"files": [
"dist"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/sonnet-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type { SonnetGet } from './types';
export type { SonnetGet, SonnetHead } from './types';
export { isServer, isBrowser } from './utils';
1 change: 1 addition & 0 deletions packages/sonnet-shared/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type SonnetGet = string | Element | Promise<string | Element>;
export type SonnetHead = string | Element | Promise<string | Element>;
13 changes: 13 additions & 0 deletions playground/head/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions playground/head/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "head",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "^5.2.2",
"vite": "^5.2.6"
},
"dependencies": {
"@sonnetjs/core": "*"
}
}
1 change: 1 addition & 0 deletions playground/head/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions playground/head/src/App.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { $component, SonnetComponent } from '@sonnetjs/core';

class App extends SonnetComponent {
static head() {
return /*html*/ `
<meta name="description" content="A Vite + Sonnet playground" />
`;
}

public script() {
console.log('App script');
}

public get() {
return /*html*/ `
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="https://vitejs.dev/logo.svg" class="logo" alt="Vite Logo" />
</a>
<h1>Vite</h1>
<h2>Hello Vite + Sonnet!</h2>
<p class="read-the-docs">Edit src/main.ts and save to test HMR.</p>
</div>
`;
}
}

export default $component(App);
7 changes: 7 additions & 0 deletions playground/head/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import './style.css';
import { createApp } from '@sonnetjs/core';
import Counter from './App';

const app = createApp();
app.root(Counter);
app.mount('#app');
96 changes: 96 additions & 0 deletions playground/head/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vanilla:hover {
filter: drop-shadow(0 0 2em #3178c6aa);
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
1 change: 1 addition & 0 deletions playground/head/src/typescript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions playground/head/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
23 changes: 23 additions & 0 deletions playground/head/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}

0 comments on commit 764f669

Please sign in to comment.