Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

103 webcomponents #2

Merged
merged 21 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update profiles and profile comp.
  • Loading branch information
frdsndr committed Dec 14, 2021
commit de6ba7bbfe21c8dff49aa65b47dad306d13f7035
34 changes: 28 additions & 6 deletions src/components/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@ const template = document.createElement('template');
template.innerHTML = `
<style>
.profileWrapper {
max-width: 300px;
display: inline-block;
text-align: center;
padding: 10px 10px;
}
.backDrop {
padding: 20px 20px;
background-color: #eee;
}
.profileImg {
width: 100px;
}
.job, .name {
position: relative;
padding: 10px 0 0 0;
}
</style>

<div class="profileWrapper">
<div>
<div class="backDrop">
<img class="profileImg" src="${profileSvg}" alt="Profile">
<div>Fred</div>
</div>
<div>
Developer
<div class="name">Fred</div>
</div>
<div class="job">Unknown</div>
</div>
`;

Expand All @@ -33,6 +40,21 @@ class Profile extends HTMLElement {
// create a shadow root
const shadow = this.attachShadow({ mode: 'open' });
shadow.appendChild(template.content.cloneNode(true));

// set job attribute
if (typeof this.getAttribute('job') === 'string') {
//! use exclamation mark to tell TS that this attribute is definitely set
shadow.querySelector('.job')!.innerHTML = this.getAttribute('job')!;
} else {
console.log('Job attr not set');
}

// set name attribute
if (typeof this.getAttribute('name') === 'string') {
shadow.querySelector('.name')!.innerHTML = this.getAttribute('name')!;
} else {
console.log('Name attr not set');
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/components/profiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// use a template instead of multiple createElement
const template = document.createElement('template');
template.innerHTML = `
<style>
.profilesWrapper {
max-width: 800px;
margin: 0 auto;
text-align: center;
}
</style>

<div class="profilesWrapper">
<kbsb-profile name="Fred" job="Designer"></kbsb-profile>
<kbsb-profile name="Hans" job="Developer"></kbsb-profile>
<kbsb-profile name="John" job="Manager"></kbsb-profile>
<kbsb-profile name="Vic" job="Engineer"></kbsb-profile>
</div>
`;

// create a class for the element
class Profiles extends HTMLElement {
constructor() {
// always call super first in constructor
super();

// create a shadow root
const shadow = this.attachShadow({ mode: 'open' });
shadow.appendChild(template.content.cloneNode(true));
}
}

// Define the new element
customElements.define('kbsb-profiles', Profiles);