웹컴포넌트를 파라미터를 통해 콘텐츠 제어하는 예시를 기록해둔다.
이렇게 하면 간단하게 파라미터 값으로 컴포넌트를 효과적으로 사용할 수 있다.
class FooterComponent extends HTMLElement {
static get observedAttributes() {
return ['company-logo', 'copyright', 'reg-number1', 'reg-number2', 'ceo', 'width', 'visible'];
}
constructor() {
super();
this.companyLogo = '';
this.copyright = '';
this.regNumber1 = '';
this.regNumber2 = '';
this.ceo = '';
this.width = '100%';
this.visible = true;
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue) return;
switch(name) {
case 'company-logo':
this.companyLogo = newValue;
break;
case 'copyright':
this.copyright = newValue;
break;
case 'reg-number1':
this.regNumber1 = newValue;
break;
case 'reg-number2':
this.regNumber2 = newValue;
break;
case 'ceo':
this.ceo = newValue;
break;
case 'width':
this.width = newValue;
break;
case 'visible':
this.visible = newValue === 'true';
break;
}
this.updateContent();
this.updateStyles();
}
updateContent() {
const footerContainer1 = this.querySelector('.footer-container1');
if (footerContainer1) {
footerContainer1.innerHTML = `
<div>${this.companyLogo}</div>
<div>
<div>${this.copyright}</div>
<div>${this.regNumber1}</div>
<div>
<span>${this.regNumber2}</span>
<span>${this.ceo}</span>
</div>
</div>
`;
}
}
updateStyles() {
const footer = this.querySelector('footer');
if (footer) {
footer.style.width = this.width;
footer.style.display = this.visible ? 'flex' : 'none';
}
}
connectedCallback() {
this.innerHTML = `
<footer>
<div class="footer-container1">
<!-- 이 부분은 updateContent에서 동적으로 채워집니다 -->
</div>
<!-- 나머지 footer 내용 -->
</footer>
<style>
footer {
display: flex;
justify-content: space-between;
width: var(--footer-width, 100%);
}
.footer-container1 {
display: flex;
flex-direction: column;
}
/* 나머지 스타일 내용 */
</style>
`;
this.updateContent();
this.updateStyles();
}
}
customElements.define('footer-component', FooterComponent);
컴포넌트에 값 넣는 예시
<footer-component
company-logo="My Company"
copyright="© 2025 My Company"
reg-number1="123-45-67890"
reg-number2="987-65-43210"
ceo="John Doe"
width="80%"
visible="true">
</footer-component>
js로도 제어 가능
const footer = document.querySelector('footer-component');
footer.setAttribute('width', '50%');
footer.setAttribute('visible', 'false');
'나의 개발 일지' 카테고리의 다른 글
자바스크립트를 이용한 모달(Modal) 창 켜고 끄기 (3) | 2025.01.28 |
---|---|
디자인 마켓플레이스 - 주요기능 1차 개발 + 스켈레톤 + 기본 예외처리 적용 (0) | 2025.01.28 |
디자인 마켓플레이스 - 주요기능 1차 개발 완료 (0) | 2025.01.20 |
[Vanilla JS] State와 Router로 원활한 뒤로가기 및 페이지 이동 구축 (디자인 마켓플레이스 프로젝트) (2) | 2024.12.14 |
aws S3로 파일서버 만들기 과정 - 1. I AM 및 S3 버킷 설정 (디자인 마켓플레이스 프로젝트 (4) | 2024.12.10 |