Skip to content

Commit

Permalink
refactor: withStyles() => useStyles() (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat authored Feb 15, 2022
1 parent a2bd712 commit 41f5f79
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 122 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@builder.io/qwik",
"version": "0.0.16-13",
"version": "0.0.16",
"description": "An Open-Source sub-framework designed with a focus on server-side-rendering, lazy-loading, and styling/animation.",
"scripts": {
"build": "node scripts --tsc --build --api --platform-binding --wasm",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-qwik",
"version": "0.0.16-13",
"version": "0.0.16",
"description": "Interactive CLI and API for generating Qwik projects.",
"bin": {
"create-qwik": "create-qwik"
Expand Down
12 changes: 6 additions & 6 deletions src/core/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ export function useHostElement(): Element;
// @public
export function useLexicalScope<VARS extends any[]>(): VARS;

// @alpha (undocumented)
export const useStyles$: (first: string) => void;

// @alpha (undocumented)
export function useStyles(styles: QRL<string>): void;

// @public (undocumented)
export function useTransient<OBJ, ARGS extends any[], RET>(obj: OBJ, factory: (this: OBJ, ...args: ARGS) => RET, ...args: ARGS): RET;

Expand All @@ -299,12 +305,6 @@ export const withScopedStyles$: (first: string) => void;
// @alpha (undocumented)
export function withScopedStyles(styles: QRL<string>): void;

// @alpha (undocumented)
export const withStyles$: (first: string) => void;

// @alpha (undocumented)
export function withStyles(styles: QRL<string>): void;

// Warnings were encountered during analysis:
//
// dist-dev/tsc-out/src/core/render/jsx/slot.public.d.ts:8:5 - (ae-forgotten-export) The symbol "JSXChildren" needs to be exported by the entry point index.d.ts
Expand Down
22 changes: 11 additions & 11 deletions src/core/component/component.public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,23 @@ export function onWindow(event: string, eventFn: QRL<() => void>): QRL<() => voi
throw new Error('IMPLEMENT: onWindow' + eventFn);
}

// <docs markdown="https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#withStyles">
// !!DO NOT EDIT THIS COMMENT DIRECTLY!!! (edit https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#withStyles instead)
// <docs markdown="https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#useStyles">
// !!DO NOT EDIT THIS COMMENT DIRECTLY!!! (edit https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#useStyles instead)
/**
* @alpha
*/
// </docs>
export function withStyles(styles: QRL<string>): void {
_withStyles(styles, false);
export function useStyles(styles: QRL<string>): void {
_useStyles(styles, false);
}

// <docs markdown="https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#withStyles">
// !!DO NOT EDIT THIS COMMENT DIRECTLY!!! (edit https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#withStyles instead)
// <docs markdown="https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#useStyles">
// !!DO NOT EDIT THIS COMMENT DIRECTLY!!! (edit https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#useStyles instead)
/**
* @alpha
*/
// </docs>
export const withStyles$ = implicit$FirstArg(withStyles);
export const useStyles$ = implicit$FirstArg(useStyles);

// <docs markdown="https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#withScopedStyles">
// !!DO NOT EDIT THIS COMMENT DIRECTLY!!! (edit https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#withScopedStyles instead)
Expand All @@ -202,7 +202,7 @@ export const withStyles$ = implicit$FirstArg(withStyles);
*/
// </docs>
export function withScopedStyles(styles: QRL<string>): void {
_withStyles(styles, true);
_useStyles(styles, true);
}

// <docs markdown="https://hackmd.io/c_nNpiLZSYugTU0c5JATJA#withScopedStyles">
Expand Down Expand Up @@ -289,7 +289,7 @@ export interface ComponentOptions {
* ```
*
* See also: `component`, `onUnmount`, `onHydrate`, `onDehydrate`, `onHalt`,
* `onResume`, `on`, `onDocument`, `onWindow`, `withStyles`, `withScopedStyles`
* `onResume`, `on`, `onDocument`, `onWindow`, `useStyles`, `withScopedStyles`
*
* @param onMount - Initialization closure used when the component is first created.
* @param tagName - Optional components options. It can be used to set a custom tag-name to be
Expand Down Expand Up @@ -377,7 +377,7 @@ export function component<PROPS extends {}>(
* ```
*
* See also: `component`, `onRender`, `onUnmount`, `onHydrate`, `onDehydrate`, `onHalt`,
* `onResume`, `on`, `onDocument`, `onWindow`, `withStyles`, `withScopedStyles`
* `onResume`, `on`, `onDocument`, `onWindow`, `useStyles`, `withScopedStyles`
*
* @param onMount - Initialization closure used when the component is first created.
* @param tagName - Optional components options. It can be used to set a custom tag-name to be
Expand Down Expand Up @@ -411,7 +411,7 @@ function resolveQrl<PROPS extends {}>(
});
}

function _withStyles(styles: QRL<string>, scoped: boolean) {
function _useStyles(styles: QRL<string>, scoped: boolean) {
const styleQrl = toQrlOrError(styles);
const styleId = styleKey(styleQrl);
const hostElement = useHostElement();
Expand Down
4 changes: 2 additions & 2 deletions src/core/component/component.unit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expectDOM } from '../../testing/expect-dom.unit';
import { runtimeQrl } from '../import/qrl';
import { $ } from '../import/qrl.public';
import { render } from '../render/render.public';
import { PropsOf, withStyles, component$ } from './component.public';
import { PropsOf, useStyles, component$ } from './component.public';

describe('q-component', () => {
it('should declare and render basic component', async () => {
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('q-component', () => {

/////////////////////////////////////////////////////////////////////////////
export const HelloWorld = component$(() => {
withStyles(runtimeQrl(`{}`));
useStyles(runtimeQrl(`{}`));
return $(() => {
return <span>Hello World</span>;
});
Expand Down
5 changes: 3 additions & 2 deletions src/core/import/qrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ export function stringifyQRL(qrl: QRL, element?: Element) {
parts.push('|', key, value && value.length ? '.' + value.join('.') : '')
);
const capture = qrl_.capture;
capture && capture.length && parts.push(JSON.stringify(capture));

if (capture && capture.length > 0) {
parts.push(JSON.stringify(capture));
}
const qrlString = parts.join('');
if (qrl_.chunk === RUNTIME_QRL && element) {
const qrls: Set<QRL> = (element as any).__qrls__ || ((element as any).__qrls__ = new Set());
Expand Down
4 changes: 2 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export {
on,
onDocument,
onWindow,
withStyles,
withStyles$,
useStyles,
useStyles$,
withScopedStyles,
withScopedStyles$,
} from './component/component.public';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ expression: output
==INPUT==


import { component$, withStyle$ } from '@builder.io/qwik';
import { component$, useStyles$ } from '@builder.io/qwik';
import css1 from './global.css';
import css2 from './style.css';
import css3 from './style.css';

export const App = component$(() => {
withStyle$(`${css1}${css2}`);
withStyle$(css3);
useStyles$(`${css1}${css2}`);
useStyles$(css3);
})

============================= test.js ==
Expand All @@ -24,46 +24,46 @@ import { component } from "@builder.io/qwik";
export const App = /*#__PURE__*/ component(qwik.qrl(()=>import("./h_test_app_onmount")
, "App_onmount"));

============================= h_test_app_withstyle.js (ENTRY POINT)==
============================= h_test_app_usestyles.js (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
import css1 from "./global.css";
import css2 from "./style.css";
export const App_withStyle = `${css1}${css2}`;
export const App_useStyles = `${css1}${css2}`;

/*
{
"origin": "test.tsx",
"name": "App_withStyle",
"name": "App_useStyles",
"entry": null,
"canonicalFilename": "h_test_app_withstyle",
"canonicalFilename": "h_test_app_usestyles",
"extension": "js"
}
*/
============================= h_test_app_withstyle2.js (ENTRY POINT)==
============================= h_test_app_usestyles2.js (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
import css3 from "./style.css";
export const App_withStyle2 = css3;
export const App_useStyles2 = css3;

/*
{
"origin": "test.tsx",
"name": "App_withStyle2",
"name": "App_useStyles2",
"entry": null,
"canonicalFilename": "h_test_app_withstyle2",
"canonicalFilename": "h_test_app_usestyles2",
"extension": "js"
}
*/
============================= h_test_app_onmount.js (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
import { withStyle } from "@builder.io/qwik";
import { useStyles } from "@builder.io/qwik";
export const App_onmount = ()=>{
withStyle(qwik.qrl(()=>import("./h_test_app_withstyle")
, "App_withStyle"));
withStyle(qwik.qrl(()=>import("./h_test_app_withstyle2")
, "App_withStyle2"));
useStyles(qwik.qrl(()=>import("./h_test_app_usestyles")
, "App_useStyles"));
useStyles(qwik.qrl(()=>import("./h_test_app_usestyles2")
, "App_useStyles2"));
};

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ expression: output
==INPUT==


import { component$, $, withStyle$ } from '@builder.io/qwik';
import { component$, $, useStyles$ } from '@builder.io/qwik';

export const App = component$((props) => {
withStyle$('hola');
useStyles$('hola');
return $(() => (
<div></div>
));
Expand All @@ -23,17 +23,17 @@ import { component } from "@builder.io/qwik";
export const App = /*#__PURE__*/ component(qwik.qrl(()=>import("./h_test_app_onmount.tsx")
, "App_onmount"));

============================= h_test_app_withstyle.tsx (ENTRY POINT)==
============================= h_test_app_usestyles.tsx (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
export const App_withStyle = 'hola';
export const App_useStyles = 'hola';

/*
{
"origin": "test.tsx",
"name": "App_withStyle",
"name": "App_useStyles",
"entry": null,
"canonicalFilename": "h_test_app_withstyle",
"canonicalFilename": "h_test_app_usestyles",
"extension": "tsx"
}
*/
Expand All @@ -55,10 +55,10 @@ export const App_onRender = ()=><div ></div>
============================= h_test_app_onmount.tsx (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
import { withStyle } from "@builder.io/qwik";
import { useStyles } from "@builder.io/qwik";
export const App_onmount = (props)=>{
withStyle(qwik.qrl(()=>import("./h_test_app_withstyle.tsx")
, "App_withStyle"));
useStyles(qwik.qrl(()=>import("./h_test_app_usestyles.tsx")
, "App_useStyles"));
return qwik.qrl(()=>import("./h_test_app_onrender.tsx")
, "App_onRender");
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ expression: output
==INPUT==


import { component$, $, withStyle$ } from '@builder.io/qwik';
import { component$, $, useStyles$ } from '@builder.io/qwik';

export const App = component$((props) => {
withStyle$('hola');
useStyles$('hola');
return $(() => (
<div></div>
));
Expand All @@ -23,17 +23,17 @@ import { component } from "@builder.io/qwik";
export const App = /*#__PURE__*/ component(qwik.qrl(()=>import("./h_test_app_onmount.js")
, "App_onmount"));

============================= h_test_app_withstyle.js (ENTRY POINT)==
============================= h_test_app_usestyles.js (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
export const App_withStyle = 'hola';
export const App_useStyles = 'hola';

/*
{
"origin": "test.tsx",
"name": "App_withStyle",
"name": "App_useStyles",
"entry": null,
"canonicalFilename": "h_test_app_withstyle",
"canonicalFilename": "h_test_app_usestyles",
"extension": "js"
}
*/
Expand All @@ -56,10 +56,10 @@ export const App_onRender = ()=>/*#__PURE__*/ _jsx("div", {})
============================= h_test_app_onmount.js (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
import { withStyle } from "@builder.io/qwik";
import { useStyles } from "@builder.io/qwik";
export const App_onmount = (props)=>{
withStyle(qwik.qrl(()=>import("./h_test_app_withstyle.js")
, "App_withStyle"));
useStyles(qwik.qrl(()=>import("./h_test_app_usestyles.js")
, "App_useStyles"));
return qwik.qrl(()=>import("./h_test_app_onrender.js")
, "App_onRender");
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ expression: output
==INPUT==


import { $, component$, withStyle$ } from '@builder.io/qwik';
import { $, component$, useStyles$ } from '@builder.io/qwik';
import css1 from './global.css';
import css2 from './style.css';

export const App = component$(() => {
const style = `${css1}${css2}`;
withStyle$(style);
useStyles$(style);
const render = () => {
return (
<div></div>
Expand All @@ -29,17 +29,17 @@ import { component } from "@builder.io/qwik";
export const App = /*#__PURE__*/ component(qwik.qrl(()=>import("./h_test_app_onmount")
, "App_onmount"));

============================= h_test_app_withstyle.js (ENTRY POINT)==
============================= h_test_app_usestyles.js (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
export const App_withStyle = style;
export const App_useStyles = style;

/*
{
"origin": "test.tsx",
"name": "App_withStyle",
"name": "App_useStyles",
"entry": null,
"canonicalFilename": "h_test_app_withstyle",
"canonicalFilename": "h_test_app_usestyles",
"extension": "js"
}
*/
Expand All @@ -63,11 +63,11 @@ import { jsx as _jsx } from "@builder.io/qwik/jsx-runtime";
import * as qwik from "@builder.io/qwik";
import css1 from "./global.css";
import css2 from "./style.css";
import { withStyle } from "@builder.io/qwik";
import { useStyles } from "@builder.io/qwik";
export const App_onmount = ()=>{
const style = `${css1}${css2}`;
withStyle(qwik.qrl(()=>import("./h_test_app_withstyle")
, "App_withStyle"));
useStyles(qwik.qrl(()=>import("./h_test_app_usestyles")
, "App_useStyles"));
const render = ()=>{
return(/*#__PURE__*/ _jsx("div", {}));
};
Expand Down
Loading

1 comment on commit 41f5f79

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 41f5f79 Previous: ab9c1eb Ratio
transform_todo_app 1985883 ns/iter (± 297550) 1686368 ns/iter (± 23791) 1.18

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.