Skip to content

Commit

Permalink
adding classnames function
Browse files Browse the repository at this point in the history
  • Loading branch information
cblanquera committed Aug 20, 2024
1 parent 80a4d0b commit 3382e65
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/temple/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2025 Chris Blanquera

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 2 additions & 0 deletions packages/temple/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import emitter, { TempleEmitter } from './client/TempleEmitter';
import TempleException from './client/TempleException';
import data from './client/data';
import props from './client/props';
import classnames from './client/classnames';
import children, { innerHTML } from './client/children';
import signal, { SignalRegistry } from './client/signal';
import './client/helpers';

export {
data,
props,
classnames,
children,
innerHTML,
signal,
Expand Down
10 changes: 10 additions & 0 deletions packages/temple/src/client/classnames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type TempleComponent from './TempleComponent';
import props from './props';

/**
* Get the current classnames where this is being called from
* ie. const classes = classnames();
*/
export default function classnames(component: TempleComponent|null = null) {
return props<{'class': string}>(component)['class'];
}
2 changes: 1 addition & 1 deletion packages/temple/src/client/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export default function props<
if (component) {
return component.props as T;
}
return {};
return {} as T;
}
4 changes: 4 additions & 0 deletions packages/temple/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import definitions, {
import {
camelize,
capitalize,
decrypt,
encrypt,
lowerlize,
slugify,
serialize,
Expand Down Expand Up @@ -77,6 +79,8 @@ export {
identifier,
camelize,
capitalize,
decrypt,
encrypt,
lowerlize,
slugify,
serialize,
Expand Down
28 changes: 23 additions & 5 deletions packages/temple/src/component/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,42 @@ export default class Component {
this._components = this.ast.components.map(component => {
//get property attributes
const properties = component.attributes.properties;
//find type property
const property = properties.find(
//find type property
// ie. <link type="component" />
// ie. <link type="template" />
const typeProperty = properties.find(
property => property.key.name === 'type'
);
//determine the type of component
const type = (
//if property is found
property
typeProperty
//and the value type is a literal
&& property.value.type === 'Literal'
&& typeProperty.value.type === 'Literal'
//and the value is 'template'
&& property.value.value === 'template'
&& typeProperty.value.value === 'template'
) ? 'template' : 'component';

//find name property
// ie. <link name="foo-bar" />
const nameProperty = properties.find(
property => property.key.name === 'name'
);
//determine the type of component
const name = (
//if property is found
nameProperty
//and the value type is a literal
&& nameProperty.value.type === 'Literal'
//and type of value is string
&& typeof nameProperty.value.value === 'string'
) ? nameProperty.value.value : undefined;

return new Component(component.source.value, {
cwd: this._cwd,
fs: this._fs,
seed: this._seed,
name: name,
type: type
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/temple/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TempleText from './server/TempleText';
import TempleServerException from './server/TempleException';
import data from './server/data';
import props from './server/props';
import classnames from './server/classnames';
import signal from './server/signal';

export type * from './types';
Expand All @@ -15,6 +16,7 @@ export {
data,
emitter,
props,
classnames,
signal,
TempleCollection,
TempleDocument,
Expand Down
8 changes: 4 additions & 4 deletions packages/temple/src/server/TempleDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default abstract class TempleDocument {
* Returns the rendered document with build files
*/
public build(path: string, props: Record<string, any> = {}) {
let document = this.render(props);
let document = this.markup(props);
const id = this.id();
const styles = this.styles().trim();
const inject = [
Expand All @@ -50,8 +50,8 @@ export default abstract class TempleDocument {
/**
* Returns the rendered document with inline code
*/
public inline(props: Record<string, any> = {}) {
const document = this.render(props);
public render(props: Record<string, any> = {}) {
const document = this.markup(props);
const styles = this.styles().trim();
const inject = [
`<script class="temple-build">${this.props()}</script>`,
Expand All @@ -76,7 +76,7 @@ export default abstract class TempleDocument {
/**
* Renders the redered document without injections
*/
public render(props: Record<string, any> = {}) {
public markup(props: Record<string, any> = {}) {
//set props (this is so template() can read it)
data.set('props', props || {});
//set the current component (this is so template() can read it)
Expand Down
9 changes: 9 additions & 0 deletions packages/temple/src/server/classnames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import props from './props';

/**
* Get the current classnames where this is being called from
* ie. const classes = classnames();
*/
export default function classnames() {
return props<{'class': string}>()['class'];
}

0 comments on commit 3382e65

Please sign in to comment.